Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Infinite Loop UGH
    primarykey
    data
    text
    <p>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.</p> <pre><code> &lt;?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 3 (if user chooses to adjust item quantity) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['item_to_adjust']) &amp;&amp; $_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 &gt;= 100) { $quantity = 99; } if ($quantity &lt; 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" &amp;&amp; $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" =&gt; $item_to_adjust, "quantity" =&gt; $quantity))); } // close if condition } // close while loop } // close foreach loop } ?&gt; </code></pre> <p>I just want it to update the quantity of the existing product, not add it as another. Thanks in advance for all the help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload