I have a piece of code which causes an endless loop but only in certain circumstances.
It's for a shopping cart quantity change and at the moment the cart works correctly when changing the quantity of the last item added. but for example if I have 3 items in the cart i can not change the quantity of the 1st or 2nd item because the loops runs endlessly.
Im not sure what is wrong with this code Ive found similar problems but no solution.
The code looks like this:
foreach ($_SESSION["cart"] 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"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
} // close if condition
} // close while loop
if ($i > 50) die("manual termination");
} // close foreach loop
If i do a var_dump on the SESSION when i have added 2 items to the cart it displays the following:
array(2) { [0]=> array(2) { ["item_id"]=> string(11) "100-C09EJ01" ["quantity"]=> string(1) "3" } [1]=> array(2) { ["item_id"]=> string(11) "700-CF220EJ" ["quantity"]=> int(1) } }
Could someone help me please?
Thankyou in advance.