Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php session_start(); ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta name="description" content="PHP Shopping Cart Using Sessions" />
- <meta name="keywords" content="shopping cart tutorial, shopping cart, php, sessions" />
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <link rel="stylesheet" media="all" href="/style/style.css" type="text/css" />
- <title>Cart</title>
- <style>
- body {
- background-color: aliceblue;
- }
- .aside {
- display: block;
- float: right;
- width:30%;
- }
- main {
- display: block;
- float: left;
- width: 70%;
- }
- header {
- height:50px;
- background:#8c8c8c;
- max-width: none;
- }
- .nav {
- list-style-type: none;
- margin: 0;
- padding: 0;
- overflow: hidden;
- background-color: #333;
- }
- li {
- float: left;
- }
- li a {
- display: block;
- color: white;
- text-align: center;
- padding: 14px 16px;
- text-decoration: none;
- }
- footer {
- position: absolute;
- right: 0;
- bottom: 0;
- left: 0;
- padding: 1rem;
- background-color: aliceblue;
- text-align: center;
- }
- </style>
- <body>
- <header>
- <h1>Bobs slakt</h1>
- </header>
- <ul class="nav">
- <li>
- <a href="index.php">Home</a>
- </li>
- <li>
- <a href="sqlproducts.php">Products</a>
- </li>
- <li>
- <a href="admin.html">Admin</a>
- </li>
- </ul>
- <main>
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- try {
- $conn = new PDO ("mysql:host=$servername;dbname=webshop", $username, $password);
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- catch(PDOException $e)
- {
- echo "Connection failed: " . $e->getMessage();
- }
- //echo "Connection completed";
- ?>
- <?php
- $product_id = $_GET['id']; //the product id from the URL
- $action = $_GET['action']; //the action from the URL
- //if there is an product_id and that product_id doesn't exist display an error message
- switch($action) { //decide what to do
- case "add":
- $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id
- break;
- case "remove":
- $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id
- 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.
- break;
- case "empty":
- unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart.
- break;
- }
- ?>
- <?php
- if($_SESSION['cart']) { //if the cart isn't empty
- //show the cart
- echo "<table border=\"1\" padding=\"3\" width=\"40%\">"; //format the cart using a HTML table
- $total=0;
- //iterate through the cart, the $product_id is the key and $quantity is the value
- foreach($_SESSION['cart'] as $product_id => $quantity) {
- //get the name, description and price from the database - this will depend on your database implementation.
- //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
- $sql = sprintf("SELECT name, price FROM products WHERE id = %d;",
- $product_id);
- $result = $conn->query($sql);
- if($result->rowCount() > 0) {
- list($name, $price) = $result->fetch();
- $line_cost = $price * $quantity; //work out the line cost
- $total = $total + $line_cost; //add to the total cost
- echo "<tr>";
- //show this information in table cells
- echo "<td align=\"center\">$name</td>";
- //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
- echo "<td align=\"center\">$quantity <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$product_id\">X</a></td>";
- echo "<td align=\"center\">$line_cost kr</td>";
- echo "</tr>";
- }
- }
- //show the total
- echo "<tr>";
- echo "<td colspan=\"2\" align=\"right\">Total</td>";
- echo "<td align=\"right\">$total kr</td>";
- echo "</tr>";
- //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
- echo "<tr>";
- echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>";
- echo "</tr>";
- echo "</table>";
- }else{
- //otherwise tell the user they have no items in their cart
- echo "You have no items in your shopping cart.";
- }
- ?>
- <a href="sqlproducts.php">Continue Shopping</a>
- </main>
- </body>
- <footer>
- © Copyright 2016 - Oscar Lejerbäck Wolf
- </footer>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement