SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php session_start();
  2.  
  3. $products = array("Oranges", "apples", "tomatoes");
  4. $amounts = array("19.99", "10.99", "2.99");
  5.  
  6. //Load up session
  7.  if ( !isset($_SESSION["total"]) ) {
  8.    $_SESSION["total"] = 0;
  9.    for ($i=0; $i< count($products); $i++) {
  10.    $_SESSION["amounts"][$i] = 0;
  11.   }
  12.  }
  13.  
  14.  //---------------------------
  15.  
  16.  //---------------------------
  17.  //Add
  18.  if ( isset($_GET["add"]) )
  19.    {
  20.    $i = $_GET["add"];
  21.    $_SESSION["amounts"][$i] = $amounts[$i];
  22.    $_SESSION["cart"][$i] = $i;
  23.  }
  24.  
  25.  ?>
  26.  
  27.  <table>
  28.    <tr>
  29.    <th>Product</th>
  30.    <th width="10px">&nbsp;</th>
  31.    <th>Amount</th>
  32.    <th width="10px">&nbsp;</th>
  33.    <th>Action</th>
  34.    </tr>
  35.  <?php
  36.  for ($i=0; $i< count($products); $i++) {
  37.    ?>
  38.    <tr>
  39.    <td><?php echo($products[$i]); ?></td>
  40.    <td width="10px">&nbsp;</td>
  41.    <td><?php echo($amounts[$i]); ?></td>
  42.    <td width="10px">&nbsp;</td>
  43.    <td><a href="?add=<?php echo($i); ?>">Add to cart</a></td>
  44.    </tr>
  45.    <?php
  46.  }
  47.  ?>
  48.  <tr>
  49.  <td colspan="5"></td>
  50.  </tr>
  51.  </table>
  52.  <?php
  53.  if ( isset($_SESSION["cart"]) ) {
  54.  ?>
  55.  <br/><br/><br/>
  56.  <h2>Cart</h2>
  57.  <table>
  58.  <tr>
  59.  <th>Product</th>
  60.  <th width="10px">&nbsp;</th>
  61.  <th>Amount</th>
  62.  <th width="10px">&nbsp;</th>
  63.  <th>Action</th>
  64.  </tr>
  65.  <?php
  66.  $total = 0;
  67.  foreach ( $_SESSION["cart"] as $i ) {
  68.  ?>
  69.  <tr>
  70.  <td><?php echo( $products[$_SESSION["cart"][$i]] ); ?></td>
  71.  <td width="10px">&nbsp;</td>
  72.  <td><?php echo( $_SESSION["amounts"][$i] ); ?></td>
  73.  <td width="10px">&nbsp;</td>
  74.  </tr>
  75.  <?php
  76.  $total = $total + $_SESSION["amounts"][$i];
  77.  }
  78.  $_SESSION["total"] = $total;
  79.  ?>
  80.  <tr>
  81.  <td colspan="7">Total : <?php echo($total); ?></td>
  82.  </tr>
  83.  </table>
  84.  <?php
  85.  }
  86.  ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top