title: Shopping cart: adding more items into the cart array
body: <p>How can I add more than one items into a shopping cart?</p>
  
  <p>This is the html form,</p>
  
  <pre><code>&lt;form action="cart.php?action=add" method="post" id="form-cart"&gt;
      &lt;input type="text" name="cart[unique_id]" value="&lt;?php echo $product-&gt;unique_id;?&gt;" size="3" maxlength="3" /&gt;
      &lt;input type="text" name="cart[quantity_stock]" value="&lt;?php echo $product-&gt;in_stock;?&gt;" size="3" maxlength="3" /&gt;
      &lt;input type="submit" value="Add to cart" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>this is the <code>add_item</code> method in the <code>cart</code> class,</p>
  
  <pre><code> public function add_item($info)
  {
  
      # Check if the content array does not exist yet.
      if (isset($this-&gt;content[0]['unique_id'])) 
      {
          $i = 0;
          $this-&gt;content[$i]['unique_id'] = $this-&gt;content[$i]['unique_id'];
          $this-&gt;content[$i]['quantity_request']++;
          $this-&gt;content[$i]['quantity_stock'] = $this-&gt;content[$i]['quantity_stock'];
          $this-&gt;content[$i]['time_created'] = time();
          $i += $i;
  
          # Check if the quantity request exceeds quanity in stock.
          if($this-&gt;content[$i]['quantity_request'] &gt; $this-&gt;content[$i]['quantity_stock'])
          {
              # If it does, then set the quantity request to quanity in stock.
              $this-&gt;content[$i] = array(
                  'unique_id' =&gt; $this-&gt;content[$i]['unique_id'], 
                  'quantity_request' =&gt; $this-&gt;content[$i]['quantity_stock'], 
                  'quantity_stock' =&gt; $this-&gt;content[$i]['quantity_stock'],
                  'time_created' =&gt; time(),
                  'error' =&gt; 'The quantity you requested on this item is exceeding the quantity in our stock.'
              );
          }
      } 
      else 
      {
          //$this-&gt;content[$unique_id] = array('quantity_request' =&gt; 1, 'quantity_stock' =&gt; $this-&gt;content[$unique_id]['quantity_stock'],'time_created' =&gt; time());
  
          # Create the content array, $unique_id as the key.
          # If it does, then set the quantity request to quanity in stock.
          $this-&gt;content[0] = array(
              'unique_id' =&gt; $info['unique_id'],
              'quantity_request' =&gt; 1,
              'quantity_stock' =&gt; $info['quantity_stock'],
              'time_created' =&gt; time()
          );
      }
  
  }
  </code></pre>
  
  <p>So, after I have an item already in the class,</p>
  
  <pre><code>array
    0 =&gt; 
      array
        'unique_id' =&gt; string '1386638969582999' (length=16)
        'quantity_request' =&gt; int 1
        'quantity_stock' =&gt; string '10' (length=2)
        'time_created' =&gt; int 1322429496
  </code></pre>
  
  <p>The array should have increased another item when I submit another item like this,</p>
  
  <p>html,</p>
  
  <pre><code>&lt;form action="cart.php?action=add" method="post" id="form-cart"&gt;
      &lt;input type="text" name="cart[&lt;?php echo $product-&gt;unique_id;?&gt;][quantity_stock]" value="&lt;?php echo $product-&gt;in_stock;?&gt;" size="3" maxlength="3" /&gt;
      &lt;input type="submit" value="Add to cart" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>php,</p>
  
  <pre><code>array
    0 =&gt; 
      array
        'unique_id' =&gt; string '1386638969582999' (length=16)
        'quantity_request' =&gt; int 1
        'quantity_stock' =&gt; string '11' (length=2)
        'time_created' =&gt; int 1322429496
  
    1 =&gt; 
      array
        'unique_id' =&gt; string '1386638969582900' (length=16)
        'quantity_request' =&gt; int 1
        'quantity_stock' =&gt; string '10' (length=2)
        'time_created' =&gt; int 1322429400
  </code></pre>
  
  <p>But I always get one item only with the increasing requested quantity,</p>
  
  <pre><code>array
        0 =&gt; 
          array
            'unique_id' =&gt; string '1386638969582999' (length=16)
            'quantity_request' =&gt; int 2
            'quantity_stock' =&gt; string '10' (length=2)
            'time_created' =&gt; int 1322429496
  </code></pre>
  
  <p>Note that it now became <strong>2</strong> in <code>'quantity_request' =&gt; int 2</code></p>
  
  <p>The <code>quantity_request</code> should only increased if the same <code>unique_id</code> is clicked again.</p>
  
  <p>How can I make this right?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>html,</p>
  
  <pre><code>&lt;form action="cart.php?action=add" method="post" id="form-cart"&gt;
      &lt;input type="text" name="cart[&lt;?php echo $product-&gt;unique_id;?&gt;][quantity_stock]" value="&lt;?php echo $product-&gt;in_stock;?&gt;" size="3" maxlength="3" /&gt;
      &lt;input type="submit" value="Add to cart" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>php,</p>
  
  <pre><code>public function add_item($item_array)
      {
  
          foreach ($item_array as $unique_id =&gt; $info)
          {
  
              # Check if the content array does not exist yet.
              if (isset($this-&gt;content[$unique_id])) 
              {
                  $this-&gt;content[$unique_id]['quantity_request']++;
                  $this-&gt;content[$unique_id]['quantity_stock'] = $this-&gt;content[$unique_id]['quantity_stock'];
                  $this-&gt;content[$unique_id]['time_created'] = time();
  
                  # Check if the quantity request exceeds quanity in stock.
                  if($this-&gt;content[$unique_id]['quantity_request'] &gt; $this-&gt;content[$unique_id]['quantity_stock'])
                  {
                      # If it does, then set the quantity request to quanity in stock.
                      $this-&gt;content[$unique_id] = array(
                          'quantity_request' =&gt; $this-&gt;content[$unique_id]['quantity_stock'], 
                          'quantity_stock' =&gt; $this-&gt;content[$unique_id]['quantity_stock'],
                          'time_created' =&gt; time(),
                          'error' =&gt; 'The quantity you requested on this item is exceeding the quantity in our stock.'
                      );
                  }
              } 
              else 
              {
                  //$this-&gt;content[$unique_id] = array('quantity_request' =&gt; 1, 'quantity_stock' =&gt; $this-&gt;content[$unique_id]['quantity_stock'],'time_created' =&gt; time());
  
                  # Create the content array, $unique_id as the key.
                  # If it does, then set the quantity request to quanity in stock.
                  $this-&gt;content[$unique_id] = array(
                      'quantity_request' =&gt; 1,
                      'quantity_stock' =&gt; $info['quantity_stock'],
                      'time_created' =&gt; time()
                  );
              }
  
          }
      }
  </code></pre>