title: Php Shopping Cart Update quantity using Sessions
body: <p>Another solution I came up with after posting this question instead of using a for loop to add one each time I could have just said:</p>
  
  <pre><code>$session-&gt;cart[$params-&gt;id] =&gt; $qty;
  </code></pre>
  
  <p>I found this to be a better way because you can update the cart this way instead of adding the desired number onto what is already in the cart.</p>
  
  <p>For all reading this post I came up with a solution to update the cart using the handler. it is as follows . . . 
  first in the form part of details.php</p>
  
  <pre><code>&lt;form method="post"&gt; //should be added to retrieve the qty data from the text field.
  </code></pre>
  
  <p>next in the handler . . .add the follwing loop and variable</p>
  
  <pre><code>$qty = $_POST['qty']; or $qty = $_REQUEST['qty'];
  </code></pre>
  
  <p>then</p>
  
  <pre><code>for($i =0; $i &lt; $qty ; $i++){
    ++$session-&gt;cart[$params-&gt;id];
  }
  </code></pre>
  
  <hr>
  
  <p>I am creating a shopping cart website using php to handle some tasks. I am having difficulty changing the quantity of an item in the cart. here is my code that i use to get the input handle the submission and display the quantity in a cart view</p>
  
  <p>details.php: </p>
  
  <pre><code>    &lt;form id="cart_form" action="handler-add-cart.php"&gt;
       &lt;input type="hidden" name="id" value="&lt;?php echo $product-&gt;id ?&gt;" /&gt;
       &lt;input type="submit" value="add to cart"/&gt;
      **Quantity:&lt;input type="text" name="qty" /&gt;**
      &lt;/form&gt;
  </code></pre>
  
  <p>handler_add_cart.php:</p>
  
  <pre><code>&lt;?php
  require_once "include/Session.php";
  $session = new Session();
  **$params = (object) $_REQUEST;
  ++$session-&gt;cart[$params-&gt;id];**
  header("location: cart.php");
  </code></pre>
  
  <p>cart.php:</p>
  
  <pre><code> &lt;?php
      require_once "include/Session.php";
      $session = new Session();
      require_once "include/db.php";
  
      // The $cart array simplifies the view generation below, keeping 
      // computations and database accesses in this controller section.
      $cart = array();
      if (isset($session-&gt;cart)) {
      $total = 0;
      foreach ($session-&gt;cart as $prod_id =&gt; $qty) {
      $product = R::load("products", $prod_id);
      $total += $qty * $product-&gt;price;
  
      $entry = new stdClass();  // entry will contain info for table
      $entry-&gt;id = $prod_id;
      $entry-&gt;price = $product-&gt;price;
      $entry-&gt;name = $product-&gt;name;
      **$entry-&gt;qty =  $qty ;**
      $cart[] = $entry;
      }
      }
      ?&gt;
  </code></pre>
  
  <p>// here i have removed some html to focus in on my issue i have all the tags in the files so that is not the issue</p>
  
  <pre><code>    &lt;h2&gt;Cart&lt;/h2&gt;
  
      &lt;?php if (count($cart)): ?&gt;
  
       &lt;table id="display"&gt;
        &lt;tr&gt;
         &lt;th&gt;product&lt;/th&gt;&lt;th&gt;id&lt;/th&gt;&lt;th&gt;quantity&lt;/th&gt;&lt;th class='price'&gt;price&lt;/th&gt;
        &lt;/tr&gt;
        &lt;?php foreach ($cart as $entry): ?&gt;
         &lt;tr&gt;
          &lt;td&gt;&lt;a href="details.php?id=&lt;?php echo $entry-&gt;id ?&gt;"
              &gt;&lt;?php echo $entry-&gt;name ?&gt;&lt;/a&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php echo $entry-&gt;id ?&gt;&lt;/td&gt;
          **&lt;td class='qty'&gt;&lt;?php echo $entry-&gt;qty ?&gt;&lt;/td&gt;**
  
               i cleared these fields below to not distract from the issue im having
          &lt;td &gt;
           &lt;/td&gt;
         &lt;/tr&gt;
        &lt;?php endforeach ?&gt;
        &lt;tr&gt;
         &lt;th &gt;
  
         &lt;/th&gt;
        &lt;/tr&gt;
       &lt;/table&gt;
  
  
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>