PDA

View Full Version : PHP Print array on new page



hoj78
September 9th, 2008, 12:12 AM
Hi All,

I have been making a page to display records from a music database, I have a page that displays music by genre with a hyperlink to display all tracks from that genre with a checkbox, the artist name and the title. I need the user to be able to select multiple tracks and press a "add to playlist" button that stores the selection which can be emailed to the site owner. I have pasted the code below that displays all tracks from a genre with a checkbox for each. I guess I need to know what the best way to do it is.


<?php require_once('Connections/dbconn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$colname_Recordset1 = "-1";
if (isset($_GET['genre'])) {
$colname_Recordset1 = $_GET['genre'];
}
mysql_select_db($database_dbconn, $dbconn);
$query_Recordset1 = sprintf("SELECT recordID, artist, title FROM tblrecord WHERE genre = %s ORDER BY artist ASC", GetSQLValueString($colname_Recordset1, "text"));
$Recordset1 = mysql_query($query_Recordset1, $dbconn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="add_to_cart" name="add_to_cart" method="post" action="music-display.php">
<table width="68%" border="0" cellspacing="0" cellpadding="0">
<?php do { ?>
<tr>
<td width="3%"><label>
<input type="checkbox" name="recordID[]" id="checkbox" />
</label></td>
<td width="29%"><?php echo $row_Recordset1['artist']; ?></td>
<td width="68%"><?php echo $row_Recordset1['title']; ?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table><br />
<label>
<input type="submit" name="addToCartbtn" id="addToCartbtn" value="Add to my playlist" />
</label>
</form>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

adroit
September 9th, 2008, 08:24 AM
Hi hoj78,

You can use below givem code. I have done one change. You will get recordID in recordID array when u will submit the page.

Use:
<?php
echo "<pre>";
print_r($_REQUEST['recordID']);
echo "</pre>";
?>
By using above code you can view the recordID's in array format.


<?php require_once('Connections/dbconn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$colname_Recordset1 = "-1";
if (isset($_GET['genre'])) {
$colname_Recordset1 = $_GET['genre'];
}
mysql_select_db($database_dbconn, $dbconn);
$query_Recordset1 = sprintf("SELECT recordID, artist, title FROM tblrecord WHERE genre = %s ORDER BY artist ASC", GetSQLValueString($colname_Recordset1, "text"));
$Recordset1 = mysql_query($query_Recordset1, $dbconn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="add_to_cart" name="add_to_cart" method="post" action="music-display.php">
<table width="68%" border="0" cellspacing="0" cellpadding="0">
<?php do { ?>
<tr>
<td width="3%"><label>
<input type="checkbox" name="recordID[]" id="checkbox" value="<?=$row_Recordset1['recordID']?>" />
</label></td>
<td width="29%"><?php echo $row_Recordset1['artist']; ?></td>
<td width="68%"><?php echo $row_Recordset1['title']; ?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table><br />
<label>
<input type="submit" name="addToCartbtn" id="addToCartbtn" value="Add to my playlist" />
</label>
</form>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>


Regards,
Adroit
www.adroitsoftware.org








Hi All,

I have been making a page to display records from a music database, I have a page that displays music by genre with a hyperlink to display all tracks from that genre with a checkbox, the artist name and the title. I need the user to be able to select multiple tracks and press a "add to playlist" button that stores the selection which can be emailed to the site owner. I have pasted the code below that displays all tracks from a genre with a checkbox for each. I guess I need to know what the best way to do it is.


<?php require_once('Connections/dbconn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$colname_Recordset1 = "-1";
if (isset($_GET['genre'])) {
$colname_Recordset1 = $_GET['genre'];
}
mysql_select_db($database_dbconn, $dbconn);
$query_Recordset1 = sprintf("SELECT recordID, artist, title FROM tblrecord WHERE genre = %s ORDER BY artist ASC", GetSQLValueString($colname_Recordset1, "text"));
$Recordset1 = mysql_query($query_Recordset1, $dbconn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="add_to_cart" name="add_to_cart" method="post" action="music-display.php">
<table width="68%" border="0" cellspacing="0" cellpadding="0">
<?php do { ?>
<tr>
<td width="3%"><label>
<input type="checkbox" name="recordID[]" id="checkbox" />
</label></td>
<td width="29%"><?php echo $row_Recordset1['artist']; ?></td>
<td width="68%"><?php echo $row_Recordset1['title']; ?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table><br />
<label>
<input type="submit" name="addToCartbtn" id="addToCartbtn" value="Add to my playlist" />
</label>
</form>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

hoj78
September 10th, 2008, 08:59 AM
Hi adroit,

Thanks for the reply as this did print the record, I would like to know how I can put the recordID into a SQL query that will display a line for each recordID selected, I can get a line of details to display if there is 1 record but I can't work out how to display for multiple records


<?php require_once('Connections/dbconn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$colname_Recordset1 = "-1";
if (isset($_POST['recordID'])) {
$colname_Recordset1 = $_POST['recordID'];
}
mysql_select_db($database_dbconn, $dbconn);
$query_Recordset1 = sprintf("SELECT recordID, artist, title FROM tblrecord WHERE recordID = %s ORDER BY artist ASC", GetSQLValueString($colname_Recordset1, "int"));
$Recordset1 = mysql_query($query_Recordset1, $dbconn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="517" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="229"><?php echo $row_Recordset1['artist']; ?></td>
<td width="229"><?php echo $row_Recordset1['title']; ?></td>
</tr>
</table>

</body>
</html>