Advertisement
Guest User

Untitled

a guest
Dec 31st, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.33 KB | None | 0 0
  1. <?php session_start(); ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta name="description" content="PHP Shopping Cart Using Sessions" />
  6. <meta name="keywords" content="shopping cart tutorial, shopping cart, php, sessions" />
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8. <link rel="stylesheet" media="all" href="/style/style.css" type="text/css" />
  9. <title>Cart</title>
  10.  
  11. <style>
  12.     body {
  13.         background-color: aliceblue;
  14.     }
  15.     .aside {
  16.         display: block;
  17.         float: right;
  18.         width:30%;
  19.     }
  20.     main {
  21.         display: block;
  22.         float: left;
  23.         width: 70%;
  24.     }
  25.     header {
  26.         height:50px;
  27.         background:#8c8c8c;
  28.         max-width: none;
  29.     }
  30.     .nav {
  31.         list-style-type: none;
  32.         margin: 0;
  33.         padding: 0;
  34.         overflow: hidden;
  35.         background-color: #333;  
  36.     }
  37.     li {
  38.         float: left;
  39.     }
  40.     li a {
  41.         display: block;
  42.         color: white;
  43.         text-align: center;
  44.         padding: 14px 16px;
  45.         text-decoration: none;
  46.     }
  47.    
  48.     footer {
  49.     position: absolute;
  50.     right: 0;
  51.     bottom: 0;
  52.     left: 0;
  53.     padding: 1rem;
  54.     background-color: aliceblue;
  55.     text-align: center;
  56.     }
  57. </style>
  58. <body>
  59.     <header>
  60.             <h1>Bobs slakt</h1>
  61.     </header>
  62.             <ul class="nav">
  63.                 <li>
  64.                     <a href="index.php">Home</a>
  65.                 </li>
  66.                 <li>
  67.                     <a href="sqlproducts.php">Products</a>
  68.                 </li>
  69.                 <li>
  70.                     <a href="admin.html">Admin</a>
  71.                 </li>
  72.             </ul>
  73.    
  74. <main>
  75. <?php
  76. $servername = "localhost";
  77. $username = "username";
  78. $password = "password";
  79.  
  80. try {
  81.     $conn = new PDO ("mysql:host=$servername;dbname=webshop", $username, $password);
  82.        
  83.     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  84.         }
  85. catch(PDOException $e)
  86. {
  87.     echo "Connection failed: " . $e->getMessage();
  88. }
  89.     //echo "Connection completed";
  90.     ?>
  91.  
  92. <?php
  93.  
  94.     $product_id = $_GET['id'];   //the product id from the URL
  95.     $action     = $_GET['action']; //the action from the URL
  96.  
  97.     //if there is an product_id and that product_id doesn't exist display an error message
  98.  
  99.     switch($action) {   //decide what to do
  100.    
  101.         case "add":
  102.             $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id
  103.         break;
  104.        
  105.         case "remove":
  106.             $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id
  107.             if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items.
  108.         break;
  109.        
  110.         case "empty":
  111.             unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart.
  112.         break;
  113.    
  114.     }
  115.    
  116. ?>
  117.  
  118. <?php  
  119.  
  120.     if($_SESSION['cart']) { //if the cart isn't empty
  121.         //show the cart
  122.        
  123.         echo "<table border=\"1\" padding=\"3\" width=\"40%\">";    //format the cart using a HTML table
  124.               $total=0;
  125.             //iterate through the cart, the $product_id is the key and $quantity is the value
  126.             foreach($_SESSION['cart'] as $product_id => $quantity) {   
  127.                
  128.                 //get the name, description and price from the database - this will depend on your database implementation.
  129.                 //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
  130.                 $sql = sprintf("SELECT name, price FROM products WHERE id = %d;",
  131.                                 $product_id);
  132.                
  133.                 $result = $conn->query($sql);
  134.                 if($result->rowCount() > 0) {
  135.        
  136.                 list($name, $price) = $result->fetch();
  137.                
  138.                 $line_cost = $price * $quantity;        //work out the line cost
  139.                 $total = $total + $line_cost;           //add to the total cost
  140.        
  141.                 echo "<tr>";
  142.                 //show this information in table cells
  143.                 echo "<td align=\"center\">$name</td>";
  144.                 //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product
  145.                 echo "<td align=\"center\">$quantity <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$product_id\">X</a></td>";
  146.                 echo "<td align=\"center\">$line_cost kr</td>";
  147.                    
  148.                 echo "</tr>";
  149.                    
  150.                 }
  151.            
  152.             }
  153.            
  154.             //show the total
  155.             echo "<tr>";
  156.                 echo "<td colspan=\"2\" align=\"right\">Total</td>";
  157.                 echo "<td align=\"right\">$total kr</td>";
  158.             echo "</tr>";
  159.            
  160.             //show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
  161.             echo "<tr>";
  162.                 echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>";
  163.             echo "</tr>";      
  164.         echo "</table>";
  165.        
  166.        
  167.    
  168.     }else{
  169.         //otherwise tell the user they have no items in their cart
  170.         echo "You have no items in your shopping cart.";
  171.        
  172.     }
  173. ?>
  174.  
  175. <a href="sqlproducts.php">Continue Shopping</a>
  176. </main>
  177. </body>
  178.  
  179. <footer>
  180.     &copy; Copyright 2016 - Oscar Lejerbäck Wolf
  181. </footer>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement