Geez I don't even know how to class this problem...

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 .= ""; $cartOutput .= ' ' . $product_name . '
' . $product_name. ' '; $cartOutput .= '' . $details . ''; $cartOutput .= '' . $size . ''; $cartOutput .= '' . $color . ''; $cartOutput .= '' . $shipping . ''; $cartOutput .= '$' . $price . ''; //$cartOutput .= '$' . $price . ''; $cartOutput .= ' '; //$cartOutput .= '' . $each_item['quantity'] . ''; $cartOutput .= '' . $pricetotal . ''; $cartOutput .= ''; $cartOutput .= ''; $i++; } setlocale(LC_MONETARY, "en_US"); $cartTotal = money_format("%10.2n", $cartTotal); $cartTotal = "
Cart Total : ".$cartTotal." USD
"; // Finish the Paypal Checkout Btn $pp_checkout_btn .= ' '; } ?> Your Cart
<?php include_once("styles/template_header.php"); ?>
<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>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
  </tr> -->
</table>
<?php echo $cartTotal; ?>
<br />

Continue Shopping



<?php echo $pp_checkout_btn; ?>
<?php include_once("styles/template_footer.php");?>
[/php]

Hey,

I want to help but since i’m new here, i don’t know which previous problem you were referring to. Perhaps you can provide more details?

Regards,
developer.dex2908

Hey dex,

n/p I was having trouble adding options to an existing cart. I wasn’t properly carrying variables to the cart page and defining them in an array. The same variables I’m now having trouble with. Anyways I finally got them to carry over to the cart page and now I’m having trouble with each cart entry because the variables aren’t independent of one another in each item in the cart. Also, one of the things we covered last time was that I have a developmental learning disability which makes me a bit thicker than most, though I do tend to understand it in the end.

Hi,

I’m not sure what is your array structure that you have assigned to your $_SESSION[‘cart_array’] but i assume it is 2 level array? I meant array(s) inside an array?

Then i noticed this:
if ($wasFound == false) {
array_push($_SESSION[“cart_array”], array(“item_id” => $pid, “quantity” => 1));
}

Where you are trying to push a one level array instead of two level array.

I’m not sure this is the problem but i just noticed this. If you have a website then it would be easier for me to check.

Hope it helps.

Regards,
developer.dex2908

hey Dex, thanks for the response. here is the site address: http://www.theblack44s.com/database/index.php
username: temp
password: temp11

I’m fairly new to php and am still learning arrays. I know it’s a multidimensional array but haven’t heard the term level 2 array (which is good, i like learning more!). I really appreciate your time.

I had a quick look and what i think you should do is add the inputs for the size and colour to each item

[php]



[/php]

Hey Noodles,
I’m not sure I understand where to place the inputs.

Sorry I am about to go out but as i said i only had a quick look I maybe way off
you need to hold the size / colour in a separate var with the same id
so this exampe has 9 as the id
so we add the id number to the name of the input, the size is now 2 for the input size with id 9 , same thing for colour each input name needs to be separate for each item.

I hope you understand what I mean look at the source of your cart add a few items.

[php]






$93.92




[/php]

its almost right in your paypal script but still not right the colour input has the same name as the size input
one should be

name="size_1" value="XXXL (DAMN!!!!)">

the other should be

name="colour_1" value="Blue">

[php]






[/php]

For some reason I’m having trouble understanding where the

<input name="colour9" type="hidden" value="1"  />
              <input name="size9" type="hidden" value="2"  />  
              <input name="quantity" type="text" value="1"  maxlength="2" />
                <input name="adjustBtn9" type="submit" value="change" />
                <input name="item_to_adjust" type="hidden" value="9" />
                </form>
                </td><td>    $93.92</td><td>
                <form action="cart.php" method="post">
                <input name="deleteBtn9" type="submit" value="X" />
                <input name="index_to_remove" type="hidden" value="0" />

goes. Is this an addition to the existing

<label>Color
      <select name="color">
        <option selected="selected">Select</option>

in place of it, or an addendum to it?

At the moment you are trying to hold all the sizes / color variables in one basket you need to put them in their own baskets for them to work independently of each other that is why item_id is called on update so it know which line to edit but size and colour are not independent.

Those inputs would be in the cart php they ned to be generated as each item is added they will then hold the size for each productid so these have nothing to do with the post form now, they are to do with the cart automation on update.

each product has its own id if you add a few things to the cart size and colour do not have there own inputs they are just $size and $colour.

so each product will now have to have input with its own variable $size and the name will need to be a variable as well like.

[php]
[/php]

You want to add changes to code below
[php]$x = $i + 1;
$pp_checkout_btn .= ’




';
// Create the product array variable
$product_id_array .= “$item_id-”.$each_item[‘quantity’].",";
// Dynamic table row assembly
$cartOutput .= “

”;
$cartOutput .= ‘ ’ . $product_name . ‘
' . $product_name. ' ’;
$cartOutput .= ‘’ . $details . ‘’;
$cartOutput .= ‘’ . $size . ‘’;
$cartOutput .= ‘’ . $color . ‘’;
$cartOutput .= ‘’ . $shipping . ‘’;
$cartOutput .= ‘$’ . $price . ‘’;
//$cartOutput .= ‘$’ . $price . ‘’;
$cartOutput .= ’
[/php]

And here if you want to hold the values after update
[php]<?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]

So, I understand where they need to go (and why which is a huge step forward for me) but I tried different variations of the code you suggested and i can’t get past errors apparently in the way i’m writing it.

So am I trying to replace

[php]

[/php]

with

[php]

[/php]

?
I had tried to rearrange the values there to reflect the new block of code but couldn’t get it to work. I then tried replacing them straight and still had trouble. Syntax is my trouble, I understand why it has to be there.

[php][/php]

that biit does it for you but you see the name then changes to

[php]name=“size_’ . $x . '”[/php]

which will be the same as item 1
[php]name=“size_1”[/php]

So now we need to move the $size value to a var which is not $size it will become $_POST[‘size_1’]
Then we will have
[php] $size_1 = $_POST[‘size_1’];[/php]
[php][/php]

Which should ouput
item 1
[php]$cartOutput .= ‘

’ . $size_1 . ‘’;[/php]
item 2
[php]$cartOutput .= ‘’ . $size_2. ‘’;[/php]
item 3
[php]$cartOutput .= ‘’ . $size_3 . ‘’;[/php]

As size can only hold one value , you will have to stagger the values some how I used the PID but you could use the $x value as well but I think maybe the PID would be better as its a static value for the item.

Hey Noodles,
Sorry about the delay. I had to take a few days off from this. It’s driving me bonkers and I just got too frustrated with it. I appreciate your time so much, not to mention your patience. I thought that I followed your last post pretty well so I made the changes but it isn’t reflecting those changes the way it should. would you mind telling me if I made the right changes to this code? I have a feeling I’m close but am missing something small.

[php]
if (isset($_POST[‘pid’])&&(isset($_POST[‘size’])&&(isset($_POST[‘color’])))) {
$pid = $_POST[‘pid’];
$size = $_POST[‘size_1’];
$color = $_POST[‘color_1’];
//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_1, “color” => $color_1));

    } 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));
               }
    }

}
var_dump($_SESSION[“cart_array”] );
[/php]

and

[php]




[/php]

Did you take the size and colour from the array ?
[php] $_SESSION[“cart_array”] = array(1 => array(“item_id” => $pid, “quantity” => 1, “size” => $size, “color” => $color));[/php]

[php]
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));
               }
    }

}
var_dump($_SESSION[“cart_array”] );
[/php]

and

[php]




[/php]

that code is the same code I had in to begin with.

Maybe it was me looking at the array under that one thinking it was the one above without the size and colour added.

Any how you need to look at the bottom part not the top the top is just to put the array together and make the output after.

when editing the page like updating you need to make them all separate it need the $size var to be the same at the beginning as thats wht is being posted to the array.

but I am not sure what your array looks like with 2 items in there will have a look and come back to you when I get the chance to.

Ok very cool Noodles. I will put the site back online here right now for ya.

Alright now I’m really confused about this. I dumped the array (with one item in the cart) and got this"

array(1) { [1]=> array(4) { ["item_id"]=> string(1) "9" ["quantity"]=> int(1) ["size"]=> string(14) "Small (petite)" ["color"]=> string(5) "White" } } 

and with two items I got this

array(2) { [1]=> array(4) { ["item_id"]=> string(1) "9" ["quantity"]=> int(1) ["size"]=> string(14) "Small (petite)" ["color"]=> string(5) "White" } [2]=> array(2) { ["item_id"]=> string(2) "12" ["quantity"]=> int(1) } } 
Sponsor our Newsletter | Privacy Policy | Terms of Service