Expand|Select|Wrap|Line Numbers
- <?php
- session_start();
- // get the product id
- $DVDID = isset($_GET['DVDID']) ? $_GET['DVDID'] : "";
- $name = isset($_GET['NameOfTheDVD']) ? $_GET['NameOfTheDVD'] : "";
- /*
- * check if the 'cart' session array was created
- * if it is NOT, create the 'cart' session array
- . */
- if(!isset($_SESSION['cart'])){
- $_SESSION['cart'] = array();
- }
- // check if the item is in the array, if it is, do not add
- if(in_array($DVDID, $_SESSION['cart'])){
- // redirect to product list and tell the user it was added to cart
- header('Location: shop.php?action=exists&id' . $DVDID . '&name=' . $name);
- }
- // else, add the item to the array
- else{
- array_push($_SESSION['cart'], $DVDID);
- // redirect to product list and tell the user it was added to cart
- header('Location: shop.php?action=add&id' . $DVDID . '&name=' . $name);
- }
- ?>
Expand|Select|Wrap|Line Numbers
- <?php
- $action = isset($_GET['action']) ? $_GET['action'] : "";
- if($action=='removed'){
- echo "<div>" . $_GET['NameOfTheDVD'] . " was removed from cart.</div>";
- }
- if(isset($_SESSION['cart'])){
- $ids = "";
- foreach($_SESSION['cart'] as $DVDID){
- $ids = $ids . $DVDID . ",";
- }
- // remove the last comma
- $ids = rtrim($ids, ',');
- require "connect.php";
- $query = "SELECT DVDID, NameOfTheDVD, Quantity FROM DVD WHERE DVDID IN ({$ids})";
- $stmt = $dbhandle->prepare( $query );
- $stmt->execute();
- $num = $stmt->rowCount();
- if($num>0){
- echo "<table border='0'>";//start table
- // our table heading
- echo "<tr>";
- echo "<th class='textAlignLeft'>Product Name</th>";
- echo "<th>Quantity</th>";
- echo "<th>Action</th>";
- echo "</tr>";
- //also compute for total price
- $totalQuantity = 0;
- while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
- extract($row);
- $totalQuantity += $Quantity;
- //creating new table row per record
- echo "<tr>";
- echo "<td>{$name}</td>";
- echo "<td class='textAlignRight'>{$Quantity}</td>";
- echo "<td class='textAlignCenter'>";
- echo "<a href='removeFromCart.php?id={$DVDID}&name={$name}' class='customButton'>";
- echo "<img src='images/remove-from-cart.png' title='Remove from cart' />";
- echo "</a>";
- echo "</td>";
- echo "</tr>";
- }
- echo "<tr>";
- echo "<th class='textAlignCenter'>Total Price</th>";
- echo "<th class='textAlignRight'>{$totalQuantity}</th>";
- echo "<th></th>";
- echo "</tr>";
- echo "</table>";
- echo "<br /><div><a href='#' class='customButton'>Checkout</a></div>";
- }else{
- echo "<div>No products found in your cart. :(</div>";
- }
- }else{
- echo "<div>No products in cart yet.</div>";
- }
- ?>