Can anyone tell me why this code does not do what I want it to:
<?php
if (!isset($_COOKI E['basket'])) {
setcookie("bask et", $item_code, time()+60*60*24 *30, "coursework ", '',
0) ;
}
else {
setcookie( "basket", $_COOKIE['basket']-$item_code );
}
?>
The page GETs the variable $item_code from a URL and stores it in a cookie
okay. When I revisit the page with a different $item_code I want the code to
check if the cookie is set already and, if so, *append* the new $item_code
to the existing cookie. Problem the old cookie is overwritten by the new one
rather than appended.
I've been trying all day to get this to work and whereas some other code
I've looked at does append, mine doesn't, and I can't see where I am going
wrong. Here is the other code that does work but it's no good as it is
different t what I need:
<?php
error_reporting (1); // suppress warning messages
if ($clear)
setcookie("ckTr olleyContents", "", time()-3600);
elseif ($choice)
setcookie("ckTr olleyContents", "$ckTrolleyCont ents<li>$choice </li>");
?>
<html><head><ti tle>PHP cookie example</title>
</head><body>
<h2>Shopping trolley (php cookie version) </h2>
<form method="get" action="CookieT rolley.php">
<p>Choose an item: </p>
<p>
<input type="radio" name="choice" value="camera"/> camera<br />
<input type="radio" name="choice" value="snorkel"/> snorkel<br />
<input type="radio" name="choice" value="octopus"/> octopus<br />
<input type="radio" name="choice" value="cuddly toy"/> cuddly toy<br /><br
/>
<input type="submit" name="submit" value="Add to the trolley"/>
<input type="submit" name="clear" value="Empty the trolley"/>
</p>
</form>
<?php
if ((!$choice && !$ckTrolleyCont ents) || $clear)
echo "<p>Trolley currently empty</p>";
elseif (!$choice) {
echo "<p>Trolley contains:</p>";
echo "<ul>$ckTrolley Contents</ul>";
} else {
// NB the cookie will not contain what has been added to it
// during the current form execution
echo "<p>Trolley contains:</p>";
echo "<ul>$ckTrolley Contents<li>$ch oice</li></ul>";
}
?>
</body></html>
<?php
if (!isset($_COOKI E['basket'])) {
setcookie("bask et", $item_code, time()+60*60*24 *30, "coursework ", '',
0) ;
}
else {
setcookie( "basket", $_COOKIE['basket']-$item_code );
}
?>
The page GETs the variable $item_code from a URL and stores it in a cookie
okay. When I revisit the page with a different $item_code I want the code to
check if the cookie is set already and, if so, *append* the new $item_code
to the existing cookie. Problem the old cookie is overwritten by the new one
rather than appended.
I've been trying all day to get this to work and whereas some other code
I've looked at does append, mine doesn't, and I can't see where I am going
wrong. Here is the other code that does work but it's no good as it is
different t what I need:
<?php
error_reporting (1); // suppress warning messages
if ($clear)
setcookie("ckTr olleyContents", "", time()-3600);
elseif ($choice)
setcookie("ckTr olleyContents", "$ckTrolleyCont ents<li>$choice </li>");
?>
<html><head><ti tle>PHP cookie example</title>
</head><body>
<h2>Shopping trolley (php cookie version) </h2>
<form method="get" action="CookieT rolley.php">
<p>Choose an item: </p>
<p>
<input type="radio" name="choice" value="camera"/> camera<br />
<input type="radio" name="choice" value="snorkel"/> snorkel<br />
<input type="radio" name="choice" value="octopus"/> octopus<br />
<input type="radio" name="choice" value="cuddly toy"/> cuddly toy<br /><br
/>
<input type="submit" name="submit" value="Add to the trolley"/>
<input type="submit" name="clear" value="Empty the trolley"/>
</p>
</form>
<?php
if ((!$choice && !$ckTrolleyCont ents) || $clear)
echo "<p>Trolley currently empty</p>";
elseif (!$choice) {
echo "<p>Trolley contains:</p>";
echo "<ul>$ckTrolley Contents</ul>";
} else {
// NB the cookie will not contain what has been added to it
// during the current form execution
echo "<p>Trolley contains:</p>";
echo "<ul>$ckTrolley Contents<li>$ch oice</li></ul>";
}
?>
</body></html>
Comment