My object Cart is being reinstantiated at a bad time. I've searched like mad and read a lot, but evidently I'm still missing something about sessions. Somebody please take pity on me.
Here's a testing version. Sorry about the crazy formatting.
application.php:
<?
include("cart.php");
session_start();
session_register("SESSION");
if (!isset($SESSION)) {
echo "\$SESSION !isset, setting.<br>\n";
$SESSION = array();
} else {
echo "\$SESSION isset<br>\n";
}
if (!isset($SESSION["cart"])) {
echo "\$SESSION[\"cart\"] !isset, setting.<br>\n";
$SESSION["cart"] = new Cart;
} else {
echo "\$SESSION[\"cart\"] isset<br>\n";
}
?>
index.php:
<?
include("application.php");
?>
<font face=arial size=1>
item 1<a href=shopping_cart.php?a=A&id=1>add</a><br>
item 2<a href=shopping_cart.php?a=A&id=2>add</a><br>
item 3<a href=shopping_cart.php?a=A&id=3>add</a><br>
cart.php:
<?
class Cart
{
var $items;
function Cart()
{
$this->init();
}
function init()
{
$this->items = array();
$this->total = 0;
}
function set($id, $q)
{
$this->items[$id] = $q;
}
function add($id)
{
isset($this->items[$id]) ?
$this->items[$id] += 1 :
$this->items[$id] = 1;
}
function display()
{
for ($i=0; $i<10; $i++) {
if (isset($this->items[$i])) {
printf("%d=%d<br>\n", $i, $this->items[$i]);
}
}
echo "<br>\n";
}
}
?>
shopping_cart.php:
<?
include("application.php");
switch($a) {
case "A":
echo "add. id=$id<br>\n";
$SESSION["cart"]->add($id);
break;
}
$SESSION["cart"]->display();
?>