0

I have a cart array for a small ecommerce site i'm building and have run into a loop I can't figure out. If I have 3 different products (not sure if the number of products is relevant over 2) in my cart array (with different ID#'s) and I attempt to update the quantity of the second item, it causes an infinite loop and attempts to continuously add the product again as a new product, rather than updating the existing indentical product.

    <?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
}
?>

I just want it to update the quantity of the existing product, not add it as another. Thanks in advance for all the help!

1
  • It is kinda hard to spot the cause.. but one key note, try using a foreach instead of a while loop Using foreach is optimized for iteration over collections. Also I am not feeling this array (array(.. thing. Commented Oct 24, 2013 at 21:31

1 Answer 1

1

when you reach the array_splice command, you are possibly resetting the array pointer, so when the foreach iterates over the next item, it is actually starting from the first item again.

What I suggest you do, is set a flag, after the array_splice and break out of the while loop. Then test the flag before the next foreach iteration, and break out of that as well if it is set.

i.e.

    array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
    $breakflag=true;
    break;
}
if($breakflag){
    break;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.