Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php session_start();
- $products = array("Oranges", "apples", "tomatoes");
- $amounts = array("19.99", "10.99", "2.99");
- //Load up session
- if ( !isset($_SESSION["total"]) ) {
- $_SESSION["total"] = 0;
- for ($i=0; $i< count($products); $i++) {
- $_SESSION["amounts"][$i] = 0;
- }
- }
- //---------------------------
- //---------------------------
- //Add
- if ( isset($_GET["add"]) )
- {
- $i = $_GET["add"];
- $_SESSION["amounts"][$i] = $amounts[$i];
- $_SESSION["cart"][$i] = $i;
- }
- ?>
- <table>
- <tr>
- <th>Product</th>
- <th width="10px"> </th>
- <th>Amount</th>
- <th width="10px"> </th>
- <th>Action</th>
- </tr>
- <?php
- for ($i=0; $i< count($products); $i++) {
- ?>
- <tr>
- <td><?php echo($products[$i]); ?></td>
- <td width="10px"> </td>
- <td><?php echo($amounts[$i]); ?></td>
- <td width="10px"> </td>
- <td><a href="?add=<?php echo($i); ?>">Add to cart</a></td>
- </tr>
- <?php
- }
- ?>
- <tr>
- <td colspan="5"></td>
- </tr>
- </table>
- <?php
- if ( isset($_SESSION["cart"]) ) {
- ?>
- <br/><br/><br/>
- <h2>Cart</h2>
- <table>
- <tr>
- <th>Product</th>
- <th width="10px"> </th>
- <th>Amount</th>
- <th width="10px"> </th>
- <th>Action</th>
- </tr>
- <?php
- $total = 0;
- foreach ( $_SESSION["cart"] as $i ) {
- ?>
- <tr>
- <td><?php echo( $products[$_SESSION["cart"][$i]] ); ?></td>
- <td width="10px"> </td>
- <td><?php echo( $_SESSION["amounts"][$i] ); ?></td>
- <td width="10px"> </td>
- </tr>
- <?php
- $total = $total + $_SESSION["amounts"][$i];
- }
- $_SESSION["total"] = $total;
- ?>
- <tr>
- <td colspan="7">Total : <?php echo($total); ?></td>
- </tr>
- </table>
- <?php
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement