novice is so close to a shopping cart but need help asap

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
grahamne
Forum Newbie
Posts: 6
Joined: Mon Jun 16, 2008 2:29 pm

novice is so close to a shopping cart but need help asap

Post by grahamne »

When i try to delete an item from this shopping cart it keeps deleting by the variable of the arrays below rather than the array index of the shopping cart itself eg apple = [0] but any item i try to delete from the cart that matches the apple erases.

The empty basket function works and adding an item is fine its just deleting a particular item. If someone can help me or just tell me this shopping cart wont work i would be very grateful

I have a feeling my unseting of the array is just coded wrong


$items=array('apple','muffin','banana','orange');
$prices=array(1,2,3,4);



<?php
session_start();
if(!isset($_SESSION['cart'])){
$_SESSION['cart']=array();
}
if(isset($_GET['empty'])){
unset($_SESSION['cart']);
header ('location:'.$_SERVER['PHP_SELF'].'?'.SID);
exit();
}
if(isset($_GET['id']))
{
//unset($_SESSION['cart'][$_GET['id']]);
//header ('location:'.$_SERVER['PHP_SELF']);
//exit();

}

$items=array('apple','muffin','banana','orange');
$prices=array(1,2,3,4);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>GM Holidays | Flights to suit you</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript"src="prac.js"></script>

</head>
<body>
<p>Cart contains <?php echo count($_SESSION['cart']);?>
<a href ="shop.php">view items</a>
<h1>your shopping cart</h1>



<table>
<tr>
<th>Item</th>
<th>cost</th>
</tr>

<tbody>
<?php
$total=0;

for ($i=0; $i< count($_SESSION['cart']); $i++){
echo'<tr>';
echo'<td>'.$items[$_SESSION['cart'][$i]].'</td>';
echo'<td align="right">$';
echo'<td>'.number_format($prices[$_SESSION[cart][$i]],2).'</td>';
echo'<td><a href="'.$_SERVER['PHP_SELF'].'?id='.$i.'">Delete</a>';
echo'</tr>';
$total= $total + $prices[$_SESSION['cart'][$i]];
}
?>
</tbody>
<tfoot>
<tr>
<th align="right">Total:</th><br/>
<th align="right">$<?php echo number_format($total,2)?>
</tr>
</tfoot>
</table>
<a href="<?php echo $_SERVER['PHP_SELF'];?>?empty=1">empty basket</a>
</body>
</html>

Noob in need of saving
Grahamne
Post Reply