Hey kids. Have another unique problem (for those who remember the last mea culpa, this one is related) for ya. I had added some variable to a shopping cart (IE color, size etc…) but am now getting a strange problem related to them. As things are added to the cart the user is able to chose the color, size or other options available to them. However, if they return to shopping and add another item to the cart with the same color/size options, the cart changes the first items variables (size/color) to reflect the newest additions variables and so on. So if I have ten things in the cart, all ten items will have the same size/color variables as the last item added. I’m not even sure which code to post to accompany but i’ll put up the cart.php file and if any others are needed I’ll post them subsequently.
[php]
<?php session_start(); // Start session // Script Error Reporting //error_reporting(E_ALL); //ini_set('display_errors', '1'); // Connect to the MySQL database include "storescripts/connect_to_mysql.php"; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 1 (if user attempts to add something to the cart from the product page) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['pid'])&&(isset($_POST['size'])&&(isset($_POST['color'])))) { $pid = $_POST['pid']; $size = $_POST['size']; $color = $_POST['color']; //echo 'size = '.$size; //}else{ //echo 'size is not set'; $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(1 => array("item_id" => $pid, "quantity" => 1, "size" => $size, "color" => $color)); } 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) { // 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, "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, "quantity" => 1)); } } } // header("location: cart.php"); // exit(); // echo "$key => $value"; // var_dump($_SESSION["cart_array"] ); //error_reporting(E_ALL ^ E_NOTICE); //print_r ($_SESSION); ?> <?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) { // 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, "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 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 5 (render the cart for the user to view on the page) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $cartOutput = ""; $cartTotal = ""; $pp_checkout_btn = ''; $product_id_array = ''; //$size = $_POST['size']; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $cartOutput = "
Your shopping cart is empty
"; } else { // Start PayPal Checkout Button $pp_checkout_btn .= ' '; // 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["product_name"]; $price = $row["price"]; $details = $row["details"]; $shipping = $row["shipping"]; $pricesub = $price + $shipping; } //$size = $_POST['size']; $pricetotal = $pricesub * $each_item['quantity']; $cartTotal = $pricetotal + $cartTotal; setlocale(LC_MONETARY, "en_US"); $pricetotal = money_format("%10.2n", $pricetotal); // Dynamic Checkout Btn Assembly $x = $i + 1; $pp_checkout_btn .= ' '; // Create the product array variable $product_id_array .= "$item_id-".$each_item['quantity'].","; // Dynamic table row assembly $cartOutput .= "<br />
<table width="100%" height="60" border="1" cellpadding="6" cellspacing="0">
<tr>
<td width="18%" bgcolor="#C5DFFA"><strong>Product</strong></td>
<td width="30%" bgcolor="#C5DFFA"><strong>Product Description</strong></td>
<td width="19%" bgcolor="#C5DFFA"><strong>Size</strong></td>
<td width="19%" bgcolor="#C5DFFA"><strong>Color</strong></td>
<td width="5%" bgcolor="#C5DFFA"><strong>Shipping</strong></td>
<td width="5%" bgcolor="#C5DFFA"><strong>Unit Price</strong></td>
<td width="9%" bgcolor="#C5DFFA"><strong>Quantity</strong></td>
<td width="9%" bgcolor="#C5DFFA"><strong>Total</strong></td>
<td width="5%" bgcolor="#C5DFFA"><strong>Remove</strong></td>
</tr>
<?php echo $cartOutput; ?>
<!-- <tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr> -->
</table>
<?php echo $cartTotal; ?>
<br />
Continue Shopping
<?php echo $pp_checkout_btn; ?>