Help with my cart...

Hi all

I’ve been tasked with coding up a basic shopping cart…

here’s the “working” page

http://www.earthinke.co.uk/main_site/wallpaper.php

All seems well, but if you add a product to the cart, it goes in, but you can’t change the quantity at the cart stage? And in trying it loses the selected colour. Also when you add multiple items of the same spec it adds them as separate items rather than combining them together…

Any advice would be appriecated!!

Heres the code:

[php]<?php
SESSION_START();

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 1 (if user attempts to add something to the cart from the product page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST[‘pid’]))
{
$pid = $_POST[‘pid’];

if(isset($_POST[‘selectedcol’]))
{
$colour = $_POST[‘selectedcol’];

$wasFound = false;
$i = 0;
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
    // RUN IF THE CART IS EMPTY OR NOT SET
    $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1,"colour" => $colour));
} else {
    // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
    foreach ($_SESSION["cart_array"] as $each_item) {
          $i++;
          while (list($key, $value) = each($each_item)) {
              if ($key == "item_id" && $value == $pid && $key == "colour" && $value == $colour) {
                  // That item is in cart already so let's adjust its quantity using array_splice()
                  array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "colour" => $colour, "quantity" => $each_item['quantity'] + 1)));

                  
                  
                  $wasFound = true;
              } // close if condition
          } // close while loop
       } // close foreach loop
       if ($wasFound == false) {
           array_push($_SESSION["cart_array"], array("item_id" => $pid, "colour" => $colour, "quantity" => 1));
       }
    }
}
    header("location: cart.php");
   
exit();

}

?>

<?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 2 (if user chooses to empty their shopping cart) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") { unset($_SESSION["cart_array"]); } ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 3 (if user chooses to adjust item quantity) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") { // execute some code $item_to_adjust = $_POST['item_to_adjust']; $quantity = $_POST['quantity']; $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers if ($quantity >= 100) { $quantity = 99; } if ($quantity < 1) { $quantity = 1; } if ($quantity == "") { $quantity = 1; } $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $item_to_adjust && $key == "colour" && $value == $colour ) { // That item is in cart already so let's adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "colour" => $colour, "quantity" => $quantity))); } // close if condition } // close while loop } // close foreach loop } ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 4 (if user wants to remove an item from cart) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") { // Access the array and run code to remove that array index $key_to_remove = $_POST['index_to_remove']; if (count($_SESSION["cart_array"]) <= 1) { unset($_SESSION["cart_array"]); } else { unset($_SESSION["cart_array"]["$key_to_remove"]); sort($_SESSION["cart_array"]); } } ?> <?php include("./includes/database.php"); ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 5 (render the cart for the user to view on the page) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $cartOutput = ""; $cartTotal = ""; $pp_checkout_btn = ''; $product_id_array = ''; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $cartOutput = "

Your shopping cart is empty

"; } else { // Start the For Each loop $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $item_id = $each_item['item_id']; $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1"); while ($row = mysql_fetch_array($sql)) { $product_name = $row["prodname"]; $price = number_format($row["prodprice"], 2); $details = $row["proddesc"]; } $pricetotal = $price * $each_item['quantity']; $cartTotal = $pricetotal + $cartTotal; $pricetotal = number_format($pricetotal, 2); // Create the product array variable $product_id_array .= "$item_id-".$each_item['quantity'].$each_item['colour'].","; // Dynamic table row assembly $cartOutput .= ""; $cartOutput .= ' ' . $product_name ."
in ".$each_item['colour'].''; $cartOutput .= ''.$colour.''; $cartOutput .= '£' . $price . ''; $cartOutput .= ' '; //$cartOutput .= '' . $each_item['quantity'] . ''; $cartOutput .= '£' . $pricetotal . ''; $cartOutput .= ''; $cartOutput .= ''; $i++; } $cartTotal = number_format($cartTotal, 2); $cartTotal = "
Cart Total : £".$cartTotal."
"; } ?> <?php $page_title = "Wallpaper Collection"; $meta_desc = "Earth Ink Meta Description Here"; $meta_key = "Earth Inke, wallpaper, wall, paper, ink, earth, designer, design, handcrafted,"; ?> <?php include("./includes/head.php") ?> <?php include("./includes/nav.inc.php") ?>
<td width="100%" align="left" valign="top"  bgcolor="">
<div style="margin:24px; text-align:left;">

<br />
<table width="100%" border="0" cellspacing="0" cellpadding="6">
  <tr>
    <td width="35%" bgcolor="#C5DFFA" align="center"><strong>Product</strong></td>
    <td width="40%" bgcolor="#C5DFFA"><strong>Product Description</strong></td>
    <td width="10%" bgcolor="#C5DFFA"><strong>Unit Price</strong></td>
    <td width="15%" bgcolor="#C5DFFA"><strong>Quantity</strong></td>
    <td width="18%" bgcolor="#C5DFFA"><strong>Total</strong></td>
    <td width="10%" bgcolor="#C5DFFA" align="center"><strong>Remove</strong></td>
  </tr>
 <?php echo $cartOutput; ?>
 <!-- <tr>
    <td align="center">&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr> -->
</table>
<?php echo $cartTotal; ?>
<br />



Empty cart
Back to the range...






<?php include("./includes/footer.php") ?>[/php]

Thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service