Ok, here is my code:
<?php
//**Intializing Session***//
error_reporting(1); // suppress warning messages
session_start(); // Initialise session
session_register("basketContents = array ( );"); // Register our variables
//********create connection to database*********
//opens connection with my database. Result of connection stored in $db
$db = mysql_connect($host, $user, $passwd);
//All queries made on this database
mysql_select_db($dbName,$db);
//********Connection to database complete*******
if ($id) {
echo("<h3>Selected DVD</h3>");
echo("<strong>To purchase this item, click 'Add to Shopping Basket' Or you can continue to shop.</strong><br /><br />");
//Retreives all info on particular dvd
$result = mysql_query("SELECT * FROM dvd WHERE dvdid=$id",$db);
//retreiving the actor for this dvd
$result2 = mysql_query("select name from dvd dv , actor a, actordvd ad where dv.dvdid=$id and a.actorid = ad.actorid and ad.dvdid = dv.dvdid",$db);
$result3 = mysql_query("select name from dvd dv , director d where dv.dvdid=$id and d.dirid = dv.dirid");
$result4 = mysql_query("select name from dvd dv , producer p where dv.dvdid =$id and p.prodid = dv.prodid");
$myrow = mysql_fetch_array($result);
$price = $myrow["price"];
setcookie("ckPrice","$price");
//Store the title of the DVD in $selection
$selection = $myrow["title"];
//Add $selection to cookie. Cookie is required as when page is recalled $selection looses its value
setcookie("ckBasketContents","$selection");
$myrow2 = mysql_fetch_array($result2);
$myrow3 = mysql_fetch_array($result3);
$myrow4 = mysql_fetch_array($result4);
echo "<table border=1>\n";
echo "<tr><td>Title</td><td>Description</td><td>Genre</td><td>Duration</td><td>Actor</td><td>Director</td><td>Producer</td><td>Released</td></tr>\n";
//Prints out table contents according to coloumn name in database. Only want 'name' and 'descr',
//do not want to show 'actorid' so left last column empty. However needs to be there or
//an error will occur, as the table in the db has three columns, not two!!
printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", $myrow["title"], $myrow["descr"], $myrow['genre'], $myrow['duration'], $myrow2['name'], $myrow3['name'], $myrow4['name'], $myrow['rel']);
//diplay table
echo "</table>\n";
?>
<form method="post" action="<?php echo $PHP_SELF?>?<?=SID?>">
<input type="submit" name="add" value="Add to Shopping Basket"/>
<input type="submit" name="clear" value="Empty Shopping Basket"/>
</form>
<?php
} else {
$query = ("select * from dvd");
$result = mysql_query($query,$db);
if ($myrow = mysql_fetch_array($result)) {
// display list if there are records to display
do {
printf("<a href=\"%s?id=%s\">Title: %s<br /> Price: %s<br /> No_In_Stock %s<br /><br /></a><br>\n", $PHP_SELF, $myrow["dvdid"], $myrow["title"], $myrow["price"], $myrow["stock"]);
} while ($myrow = mysql_fetch_array($result));
} else {
// no records to display
echo "Sorry, no records were found!";
}
}
?>
<?php
if ( $add ) {
//$basketContents = $basketContents . $ckBasketContents;
setcookie("ckBasketContents","", time()-3600);
setcookie("ckPrice","", time()-3600);
$DVD[] = array (Title => $ckBasketContents,
Tax => "2" ,
Del => "2",
TPrice => $ckPrice );
print " <br /><br />";
for( $i=0; $i<count($DVD); $i++ ) {
//print_r($DVD[$i]);
foreach( $DVD[$i] as $key => $value ) {
print 'Key ='.$key.', value='.$value."<br>\n";
}
}
//Now need to add $DVD to session variable $basketContents
//$basketContents = $DVD;
}elseif($clear){
$basketContents = "";
setcookie("ckBasketContents","", time()-3600);
setcookie("ckPrice","", time()-3600);
}
?>
<?php
echo "<p>Basket contains:</p>";
echo "<ul>$basketContents</ul>";
?>
</body>
</html>
The cookie is set when $id is tru. $id is true when you click on the href link.
Hope this code is readable ok, thanks for taking a look.
Reg
Gary