I am trying to create a shopping cart using session variable and and storing elements inside an array.
It is working , but what I can fathom is why the array stores the wrong item? I would really appreciate some help if you have some time.
Cheers
Jamie
other.php
<?php
session_start();
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>
<!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>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<a href="act_additem.php?item=cheese&name=bergan">chease</a>
<a href="act_additem.php?item=peas&name=bergan">peas</a>
<br />
<?php
if(isset($cart_items))
{
for($i = 1; $i < count($cart_items); $i++)
{
$i = $i++ ;
print '<a href="act_removefromcart.php?no='.$i.'">X</a>';
print ' '.$i.' ';
print $cart_items[$i]['item'].'<br>';
}}
?>
</body>
</html>
act_additem.php
<?php
session_start( );
if(isset($cart_items))
{
$i = count($cart_items) + 1 ;
$cart_items[$i]['item'] = $item;
$cart_items[$i]['name'] = $name;
unset($i);
header("Location: other.php");
exit;
}
else
{
$i = 1;
$cart_items[$i]['item'] = $item;
$cart_items[$i]['name'] = $name;
session_register('cart_items');
}
header("Location: other.php")
?>