Hi,
First of all, glad to be back on the forums have been away for ages! Hi everyone!!!
Anyways,
I am trying to construct a simple basket system (yes I have looked at pre-made scripts, i havn't found any that meet my needs!) and am having major headaches with the session coding. I have verified that my setup is ok by doing a test harness like this:
<?PHP
session_start();
if (!$_GET['x']) {
var_dump($_SESSION);
} else {
$_SESSION['x'] = $_GET['x'];
}
?>
If i go to temp.php?x=67 (or whatever), 67 gets put into the session fine, I go back to the page after going to other pages etc. and the value is retained. However when I try my basket code it fails.
<?PHP
class cart {
var $items, $count, $total, $vat, $gtotal, $cartListing;
function cart() {
session_start();
}
function addItem($itemName, $itemNotes, $itemPrice, $cellX, $cellY) {
if (count($_SESSION) > 0) {
$count = count($_SESSION);
} else {
$count = 0;
}
echo $count;
$_SESSION[$count]['name'] = $itemName;
$_SESSION[$count]['notes'] = $itemNotes;
$_SESSION[$count]['price'] = $itemPrice;
$_SESSION[$count]['cellX'] = $cellX;
$_SESSION[$count]['cellY'] = $cellY;
var_dump($_SESSION);
}
function removeItem($itemId) {
unset($_SESSION[$itemId]);
}
function printCart() {
$count = count($_SESSION);
if ($count > 0) {
$total = 0;
$cartListing .= "<table border=0 bgcolor=\"#F4F4F4\" style=\"border: 1px solid black;\"><tr><td style=\"border-bottom: 1px solid Black;\"><center>Description</center></td><td style=\"border-bottom: 1px solid Black;\"><center>Price</center></td></tr>";
for($x = 0; $x < $count; $x++)
{
$cartListing .= "\n<tr><td>{$_SESSION[$x]['name']}<br/> {$_SESSION[$x]['notes']}</td><td valign=\"top\">£ ";
$cartListing .= number_format($_SESSION[$x]['price'], 2, '.', ',');
$cartListing .= "</td></tr>";
$total += $_SESSION[$x]['price'];
}
$vat = $total * 0.175;
$gtotal = $total + $vat;
$cartListing .= "<tr><td><P ALIGN=\"right\">Sub-Total</p></td><td>£ ";
$cartListing .= number_format($total, 2, '.', ',');
$cartListing .= "</td></tr>";
$cartListing .= "<tr><td><P ALIGN=\"right\">VAT</p></td><td>£ ";
$cartListing .= number_format($vat, 2, '.', ',');
$cartListing .= "</td></tr>";
$cartListing .= "<tr><td><P ALIGN=\"right\">Total</p></td><td>£ ";
$cartListing .= number_format($gtotal, 2, '.', ',');
$cartListing .= "</td></tr>";
$cartListing .= "</table>";
return $cartListing;
} else {
return "<table border=0 bgcolor=\"#F4F4F4\" style=\"border: 1px solid black;\"><tr><td>The Cart is empty</td></tr></table>";
}
}
}
?>
Basically at the var_dump($_SESSION) in the addCell function the values appear to be stored fine. However as soon as I navigate away from the page then the values are lost completely. Any ideas?
Cheers,
Tom