title: Create a one page shopping cart using PHP $_Session and NO SQL
body: <p>I am learning some PHP and I am trying to create a all in one page shopping cart. </p>
  
  <p>I have read into and can see the benefits of a SQL bases system, but I want to learn the basics first. In doing so I have create a all-in-one page that contains the products in an associated array, as well as a form that submits to itself.</p>
  
  <p>What I want to achieve is:</p>
  
  <ul>
  <li>A product can only be purchased once (buy button replaced with a remove button)</li>
  <li>The item and its cost are added to the cart below</li>
  <li>A user can either remove it from the cart, or the item list</li>
  <li>The total cost should be updated as required.</li>
  <li>The "checkout" button will submit item name and cost</li>
  <li>The form posts to itself and does not require any SQL</li>
  </ul>
  
  <p>My current problem is:</p>
  
  <ul>
  <li>I cannot purchase more than one item at a time, i.e. the cart only contains the last purchased item</li>
  <li>I cannot get it to "check" if an item has been purchased and if so, replace the "buy" with "remove"</li>
  <li>I cannot display the item details in the cart</li>
  <li>The checkout button does not pass any details to my test </li>
  </ul>
  
  <p>Again, I am not looking for a SQL solution yet, just a pure PHP using <code>$_SESSION</code> and <code>$_POST</code> and would like to use buttons instead of <code>&lt;a href add?&gt;</code> type links.</p>
  
  <p>Thanks for the lengthy read in advance here is the code:</p>
  
  <pre><code>&lt;?php
  session_start ();
  
  $items = array (
          'A123' =&gt; array (
                  'name' =&gt; 'Item1',
                  'desc' =&gt; 'Item 1 description...',
                  'price' =&gt; 1000 
          ),
          'B456' =&gt; array (
                  'name' =&gt; 'Item40',
                  'desc' =&gt; 'Item40 description...',
                  'price' =&gt; 2500 
          ),
          'Z999' =&gt; array (
                  'name' =&gt; 'Item999',
                  'desc' =&gt; 'Item999 description...',
                  'price' =&gt; 9999 
          ) 
  );
  
  if (! isset ( $_SESSION ['cart'] )) {
      $_SESSION ['cart'] = array ();
  }
  
  // Add
  if (isset ( $_POST ["buy"] )) {
      $_SESSION ['cart'] = $_POST;
  } 
  
  // Delete Item
  else if (isset ( $_POST ['delete'] )) { // a remove button has been clicked
      unset ( $_POST ['delete'] ); //
  } 
  
  // Empty Cart
  else if (isset ( $_POST ["delete"] )) { // remove item from cart
      unset ( $_SESSION ['cart'] );
  }
  
  ?&gt;
  &lt;form action='&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;' method='post'&gt;
      &lt;?php
          foreach ( $items as $ino =&gt; $item ) {
              $title = $item ['name'];
              $desc = $item ['desc'];
              $price = $item ['price'];
  
              echo " &lt;p&gt;$title&lt;/p&gt;";
              echo " &lt;p&gt;$desc&lt;/p&gt;";
              echo "&lt;p&gt;\$$price&lt;/p&gt;";
  
              if ($_SESSION ['cart'] == $ino) {
                  echo '&lt;img src="carticon.png"&gt;';
                  echo "&lt;p&gt;&lt;button type='submit' name='delete' value='$ino'&gt;Remove&lt;/button&gt;&lt;/p&gt;";
              } else {
                  echo "&lt;button type='submit' name='buy' value='$ino'&gt;Buy&lt;/button&gt; ";
              }
          }
      ?&gt;
  &lt;/form&gt;
  
  &lt;?php
  if (isset ( $_SESSION ["cart"] )) {
      ?&gt;
  
  &lt;form action='(omitted link)'
  target='_blank' method='post'
  enctype='application/x-www-form-urlencoded'&gt;
  &lt;table&gt;
      &lt;tr&gt;
          &lt;th&gt;Product&lt;/th&gt;
          &lt;th&gt;Price&lt;/th&gt;
          &lt;th&gt;Action&lt;/th&gt;
      &lt;/tr&gt;
      &lt;?php
  
      $total = 0;
      foreach ( $_SESSION ["cart"] as $i ) {
          ?&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;?php echo($_SESSION["cart"]); ?&gt; &lt;!--Item name--&gt;
          &lt;/td&gt;
          &lt;td&gt;price&lt;?php echo($_SESSION["price"][$i] ); ?&gt;
              &lt;!--Item cost--&gt;
          &lt;/td&gt;
          &lt;td&gt;&lt;button type='submit' name='delete' value='$ino'&gt;Remove&lt;/button&gt;
              &lt;/p&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;?php
          $total = + $_SESSION ["amounts"] [$i];
      }
      $_SESSION ["total"] = $total;
      ?&gt;
      &lt;tr&gt;
          &lt;td colspan="2"&gt;Total: $&lt;?php echo($total); ?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;input type='submit' value='Checkout' /&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;button type='submit' name='clear'&gt;Clear cart&lt;/button&gt;&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/form&gt;
  &lt;?php  } ?&gt;
  </code></pre>