I've been trying for ages to add a form field qty to an array to pass it to
my shopping cart and have finally managed it but not quite.
Please check this cart:
http://www.violasystems.com/shop_testing/
When you add an item using the form text box it doesn't add more than 1 on
the first submit. However if you go back and then add further items it's
will add the multiple products to the cart? I have three functions which
control this and can't see where I'm going wrong:
Submitting the form:
echo "<table width ='760' background='images/shopbg.gif'><form
action='show_cart.php' method='post'><tr><td width='380'> </td>";
echo "<td width='200' align='right'>";
echo "<b>Quantity required: </b><input type='text' name='formqty'
value='1' size='3'></td>";
echo "<td width='180' align='left'>";
echo "<input type='hidden' name='new' value='$ItemCode'>";
echo "<input type='image' src='images/add-to-cart.gif' border ='0' alt
='Add to cart'>";
echo "</td></tr></form></table>";
echo "<table width='760' background='images/shopbg.gif'><tr><td
width='200'> </td><td><img src='images/lower.gif'></td></tr></table>";
Processing the form:
<?
include ('products_sc_fns.php');
// The shopping cart needs sessions, so start one
session_start();
if($new)
{
//new item selected
if(!session_is_registered("cart"))
{
$cart = array();
session_register("cart");
$items = 0;
session_register("items");
$total_price = "0.00";
session_register("total_price");
}
$qty = $formqty;
if($cart[$new])
//$cart[$new]++;
$cart[$new] = $cart[$new] + $qty;
else
$cart[$new] = 1;
$total_price = calculate_price($cart);
$items = calculate_items($cart);
}
if($save)
{
foreach ($cart as $ItemCode => $qty)
{
if($$ItemCode=="0")
unset($cart[$ItemCode]);
else
$cart[$ItemCode] = $$ItemCode;
}
$total_price = calculate_price($cart);
$items = calculate_items($cart);
}
do_html_header("Your shopping cart");
//array de-bug
//print_r($cart);
if($cart&&array_count_values($cart))
display_cart($cart);
else
{
echo "<table width='760' cellpadding='0'
background='images/shopbg.gif'><tr><td width='200'> </td><td>There are
no items in your cart";
echo "</td></tr></table>";
}
$target = "index.php";
// if we have just added an item to the cart, continue shopping in that
category
if($new)
{
$details = get_product_details($new);
if($details["catid"])
$target = "show_cat.php?catid=".$details["catid"];
}
$path = $PHP_SELF;
$path = str_replace("show_cart.php", "", $path);
echo "<table width='760' cellpadding='0'
background='images/shopbg.gif'><tr><td width='200'> </td><td
align='left'>";
display_button($target, "continue-shopping", "Continue Shopping");
display_button("http://".$SERVER_NAME.$path."checkout.php",
"go-to-checkout", "Go To Checkout");
echo "</td></tr></table>";
do_html_footer();
?>
And displaying the cart:
function display_cart($cart, $change = true, $images = 1)
{
// display items in shopping cart
// optionally allow changes (true or false)
// optionally include images (1 - yes, 0 - no)
global $items;
global $total_price;
echo "<table border = 0 width = 760 cellspacing = 0
background='images/shopbg.gif'>
<form action = show_cart.php method = post>
<tr><th width='200'> </th><th colspan = ". (1+$images) ."
bgcolor=\"#146190\"><font face='Verdana,Arial' size='-1'
color='White'>Item</font></th>
<th bgcolor=\"#146190\"><font face='Verdana,Arial' size='-1'
color='White'>Price</font></th><th bgcolor=\"#146190\"><font
face='Verdana,Arial' size='-1' color='White'>Quantity</font></th>
<th bgcolor=\"#146190\"><font face='Verdana,Arial' size='-1'
color='White'>Total</font></th></tr>";
//display each item as a table row
$qty = $formqty;
foreach ($cart as $ItemCode => $qty)
{
$product = get_product_details($ItemCode);
echo "<tr><td width='200'> </td>";
if($images ==true)
{
echo "<td align = left>";
if (file_exists("images/$ItemCode.jpg"))
{
$size = GetImageSize("images/".$ItemCode.".jpg");
if($size[0]>0 && $size[1]>0)
{
echo "<img src=\"images/".$ItemCode.".jpg\" border=0 ";
echo "width = ". $size[0]/3 ." height = " .$size[1]/3 . ">";
}
}
else
echo " ";
echo "</td>";
}
echo "<td align = left>";
echo "<a href =
\"show_products.php?ItemCode=".$ItemCode."\">".$product["ItemName"]."</a>
(Code) ".$product["ItemCode"];
echo "</td><td align = center>� ".number_format($product["price"], 2);
echo "</td><td align = center>";
// if we allow changes, quantities are in text boxes
if ($change == true)
echo "<input type='text' name='$ItemCode' value='$qty' size='3'>";
else
echo $qty;
echo "</td><td align = center>�
".number_format($product["price"]*$qty,2)."</td></tr>\n";
}
// display total row
echo "<tr><th width='200'> </th>
<th colspan = ". (2+$images) ." bgcolor=\"#146190\"> </th>
<th align = center bgcolor=\"#146190\"><font face='Verdana,Arial'
size='-1' color='White'>
$items
</font></th>
<th align = center bgcolor=\"#146190\"><font face='Verdana,Arial'
size='-1' color='White'>
� ".number_format($total_price, 2).
"</font></th>
</tr>";
// display save change button
if($change == true)
{
echo "<tr><td width='200'> </td>
<td colspan = ". (2+$images) ."> </td>
<td align = right>
<input type = hidden name = save value = true>
<input type = image src = \"images/save-changes.gif\"
border = 0 alt = \"Save Changes\">
</td>
<td> </td>
</tr>";
}
echo "</form></table>";
}
Any help much appreciated.
rgds
Steve.