Nearly there with a shopping basket system (based with a multi dimensional array in a session - do database was available at the time...).
Symptoms: when items in basket session, changing quantity dropdown works fine (when clicking recalc button). When I go to add another product or remove one either within the basket or from somewhere else), the quantity dropdown for each product resets back to 1. The mistake's in the array handling somewhere. I've been at this all day with no luck.
Hopefully you can spot a mistake - it's messy and ugly code I'm afraid but it does fundamentally work - I couldn't find a system anywhere else. See code below and be gentle! (I'm not a proper coder although I've learned a lot recently)
Many many thanks,
Richard Pain
<?
// Cart array set up like this: [item number][description,quantity,reference, cost]
session_start();
function displayResultSet($resultset)
{
// recalc option
if (isset ($GET['Submit']) && $GET['Submit'] == "Recalc") {
session_unset();}
$keycount = 0;
$total = 0;
$record="0";
//Output each value as elements of a table starting a new row
//each time the key number of columns is reached
foreach ($resultset as $key => $value)
{
$valuesarray = array_values($resultset);
//if recalc option selected then refill session array
if (isset ($GET['Submit']) && $GET['Submit'] == "Recalc") {
$var=$_GET[$record];
$value[1]=$var;
$_SESSION['cart']=&$resultset;
}
else {
// $resultset[$record][1]=$value[1];
}
// add up price
$total=$total + ($value[3] * $value[1]);
$_SESSION['totaldr']=$total;
echo "<TR>\n";
$instext=0;
//display table
foreach ($value as $colvalue)
{ echo "<TD>\n";
echo " ";
// if the row is the quantity row - generate a drop down
if ($instext=="1" && $value[1] != "") { echo $record;
?> <select name="<?echo $record;?>"><?
for ($option=1;$option<"7";$option++) {
echo "<option value='$option";
if ($option==$value[1]) {echo "' selected";}
else {echo "'";}
echo ">$option</option>";
}
echo "</select>";
}
//if it's the price - add a currency before it
elseif ($instext=="3" && $value[3] != "") { echo "£". $colvalue;
}
else { echo $colvalue; }
$instext++;
echo " ";
echo "</TD>\n";
}
echo "<td>";
$record++;
echo "<a href=\"cart.php?action=remove&index=$key\"><img
src=\"../images/basket/remove.gif\" border=\"0\"></a>";
echo "</td>";
}
}
//Remove product
if (isset ($HTTP_GET_VARS['action']) && $HTTP_GET_VARS['action'] ==
"remove") {
unset($cart[$index]);
}
// Add product
if (!isset ($cart) or sizeof($cart)=='0') {
// The must always initially be the insurance product in the basket (for a driving gift site)
$insitem=array("Insurance (£20 per policy)<br><font size=\"1\">PLEASE
NOTE: WE ASSUME YOU DO REQUIRE INSURANCE FOR YOUR EXPERIENCE, PLEASE
CLICK REMOVE IF YOU DO NOT REQUIRE ANY.</font>","1","Ins","20");
$cart=array($key => &$insitem);
$HTTP_SESSION_VARS['cart']=&$cart;
}
if (isset ($HTTP_GET_VARS['action']) && $HTTP_GET_VARS['action'] ==
"add") {
$prodname = $HTTP_GET_VARS['descrip'];
$prodref = $HTTP_GET_VARS['ref'];
$price = $HTTP_GET_VARS['price'];
$quantity = "1";
$item= array ($prodname, $quantity, $prodref, $price);
$cart = &$HTTP_SESSION_VARS['cart'];
$id = sizeof($cart);
$newprod=array($id => &$item);
$cart = array_merge_recursive($cart, $newprod);
}
?>
<html>
<head>
<link href="../css/dgg.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#000000" text="#000000" leftmargin="0" topmargin="0"
marginwidth="0" marginheight="0">
<div align="center">
<table width="100%" border="0">
<tr>
<td colspan="3"><br>
<br></td>
</tr>
<tr valign="top">
<td width="20%"><div align="center"></div></td>
<td width="61%">
<p class="heading"><img src="../images/basket/title.gif"
width="212" height="36"><br>
<font size="1" color="#ffffff" style="bold">All goods ordered
are dispatched
either by 1st class post courier or recorded delivery within
48 Hours.
Please note, some items may take up to 5 working days to
arrive. For ‘Experience
Gift' orders you will receive a 12 month voucher and itinerary
presented
in a highly attracti ve wallet for you to present to the
recipient, with
details of how to sbook their experience. </font></p>
<p><br>
<p align="center"> <form name="recalc" method="get"
action="cart.php?action=recalc"> <table width="80%" border="0"
align="center">
<tr>
<td width="45%" class="subheading">Package</td>
<td width="10%" class="subheading">Quantity</td>
<td width="15%" class="subheading">Ref</td>
<td width="10%" class="subheading">Price</td>
<td width="20%"> </td>
</tr>
<tr>
<td colspan="5"><div align="left"><img
src="../images/divider.gif" width="100%" height="2"><br>
<br></td></tr>
<tr>
<?
displayResultSet($cart);
?>
<p><br>
</p>
</tr>
<tr valign="right">
<td colspan="4"></td>
<td> <div align="left"><br>
<span class="subheading">TOTAL: <font color="#ffffff">£<?
echo $_SESSION['totaldr'];?></font></span></div></td>
</tr>
</table>
<input type="submit" name="Submit" value="Recalc">
</form>
<table width="80%" border="0" align="center">
<tr valign="bottom">
<td><br>
<br>
<div align="left"><img src="../images/divider.gif"
width="100%" height="2"><br>
<br>
<a href="../frame/main.htm"><img
src="../images/basket/continue.gif" width="148" height="23" border="0"
alt="Continue shopping"></a> <a
href="../frame/main.htm" class="basket"> <img
src="../images/basket/checkout.gif" width="88" height="23" border="0"
alt="Checkout"></a></div>
</td>
</tr>
</table>
<br>
<br>
</p></td><td></td>
</table>
</body>
</html>