The olde shopping cart
Posted: Mon Mar 02, 2009 1:03 pm
Just a quick question to ask about removing one item from a shopping cart rather than an emptying it compltely. I know its something to do with looping the array but just need a snippet to get me started cheers.....
Products page
<?php
session_start();
if(!isset($_SESSION['cart'])){
$_SESSION['cart']=array();
}
if(isset($_GET['buy'])){
$_SESSION['cart'][]=$_GET['buy'];
header ('location:'.$_SERVER['PHP_SELF'].'?'.SID);
exit();
}
?>
<!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 ="cart.php">view cart</a>
<?php
$items=array('apple','muffin','banana','orange');
$prices=array(1,2,3,4);
?>
<table>
<tr>
<th>Item</th>
<th>cost</th>
</tr>
<tbody>
<?php
for ($i=0; $i< count($items); $i++){
echo'<tr>';
echo'<td>'.$items[$i].'</td>';
echo'<td>$'.number_format($prices[$i],2).'</td>';
echo'<td><a href="'.$_SERVER['PHP_SELF'].'?buy='.$i.'">buy</a></td>';
echo'</tr>';
}
echo $_SESSION['cart'];
?>
<tbody>
</table>
</body>
</html>
Shopping cart
<?php
session_start();
if(!isset($_SESSION['cart'])){
$_SESSION['cart']=array();
}
if(isset($_GET['empty'])){
unset($_SESSION['cart']);
header ('location:'.$_SERVER['PHP_SELF'].'?'.SID);
exit();
}
?>
<!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>
<?php
$items=array('apple','muffin','banana','orange');
$prices=array(1,2,3,4);
?>
<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'].'?delete='.$i.'">remove</a></td>';
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>
Products page
<?php
session_start();
if(!isset($_SESSION['cart'])){
$_SESSION['cart']=array();
}
if(isset($_GET['buy'])){
$_SESSION['cart'][]=$_GET['buy'];
header ('location:'.$_SERVER['PHP_SELF'].'?'.SID);
exit();
}
?>
<!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 ="cart.php">view cart</a>
<?php
$items=array('apple','muffin','banana','orange');
$prices=array(1,2,3,4);
?>
<table>
<tr>
<th>Item</th>
<th>cost</th>
</tr>
<tbody>
<?php
for ($i=0; $i< count($items); $i++){
echo'<tr>';
echo'<td>'.$items[$i].'</td>';
echo'<td>$'.number_format($prices[$i],2).'</td>';
echo'<td><a href="'.$_SERVER['PHP_SELF'].'?buy='.$i.'">buy</a></td>';
echo'</tr>';
}
echo $_SESSION['cart'];
?>
<tbody>
</table>
</body>
</html>
Shopping cart
<?php
session_start();
if(!isset($_SESSION['cart'])){
$_SESSION['cart']=array();
}
if(isset($_GET['empty'])){
unset($_SESSION['cart']);
header ('location:'.$_SERVER['PHP_SELF'].'?'.SID);
exit();
}
?>
<!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>
<?php
$items=array('apple','muffin','banana','orange');
$prices=array(1,2,3,4);
?>
<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'].'?delete='.$i.'">remove</a></td>';
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>