Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Start the session */
- session_start();
- /* Connect to the Database */
- $dbuser = "dsar";
- $dbpass = "(W6rIXNF";
- $db = "SSID";
- $connect = OCILogon($dbuser, $dbpass, $db);
- /* Process the actions */
- $cart = $_SESSION['cart'];
- $option = $_GET['option'];
- /* Perform the appropiate action based on the user's selection */
- switch($option)
- {
- /* Add to the cart */
- case 'add':
- /* If there is something in the cart */
- if ($cart)
- {
- /* Add the item to the end of the cart */
- $cart .= ','.$_GET['ID'];
- /* Calculate the postage */
- }
- else
- {
- /* Add the item to the cart */
- $cart = $_GET['ID'];
- }
- /* Add the cart to the session variable */
- $_SESSION['cart'] = $cart;
- break;
- /* Delete one item from the cart */
- case 'delete':
- /* If the cart exists */
- if ($cart)
- {
- /* Create an array */
- $items = explode(',',$cart);
- /* Create a new cart */
- $newcart = '';
- /* For each item */
- foreach($items as $item)
- {
- /* If the item is not equal to the one we want to remove */
- if ($_GET['ID']!= $item)
- {
- /* If the new cart isn't empty */
- if ($newcart != '')
- {
- /* Add the item */
- $newcart .= ','.$item;
- }
- else
- {
- /* Add the item to the cart */
- $newcart = $item;
- }
- }
- }
- /* Add the new cart back into the original cart */
- $cart = $newcart;
- }
- /* Add the cart to the session variable */
- $_SESSION['cart'] = $cart;
- break;
- case 'update':
- /* If the cart exists */
- if ($cart)
- {
- /* declare a new cart */
- $newcart = '';
- /* For each value */
- foreach($_POST as $key=>$value)
- {
- /* If the string is in the right format */
- if (stristr($key,'qty'))
- {
- /* Remove the qty from the string and store it in a variable */
- $id = str_replace('qty','',$key);
- /* Add the items to a new variable */
- $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
- /* empty the new cart */
- $newcart = '';
- /* for each item */
- foreach ($items as $item)
- {
- /* if the id is not equal to the item */
- if ($id != $item)
- {
- /* If the new cart is not empty */
- if ($newcart != '')
- {
- /* Add the item to the cart */
- $newcart .= ','.$item;
- }
- else
- {
- /* Add the item to the cart */
- $newcart = $item;
- }
- }
- }
- /* for each value */
- for($i = 1; $i <= $value; $i++)
- {
- /* If the new cart is not null */
- if ($newcart != '')
- {
- /* Add the id to the cart */
- $newcart .= ','.$id;
- }
- else
- {
- /* Add the id to the cart */
- $newcart = $id;
- }
- }
- }
- }
- }
- /* Copy the new cart back into the original */
- $cart = $newcart;
- /* Add the cart to the session variable */
- $_SESSION['cart'] = $cart;
- break;
- case 'empty':
- /* delete the session variable */
- unset($_SESSION['cart']);
- break;
- }
- ?>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>Shopping Cart</title>
- <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
- <link rel="stylesheet" type="text/css" href="format.css" >
- <!-- CSS to control the color of the links -->
- <style type = "text/css">
- A:link {text-decoration: underline; color: white;}
- A:visited {text-decoration: underline; color: black}
- A:hover {text-decoration: underline; color: red;}
- </style>
- </head>
- <body class ="background-main">
- <div id = "linkDiv" class = "align-center spacing background-links">
- <!-- The heading of the website -->
- <h1 style = "word-spacing:0px">Disarm Anime Warehouse</h1>
- <!-- A list of links to the other pages -->
- <a href = "ass2.php">Home</a>
- <a href = "information.php">Information</a>
- <a href = "product.php">Products</a>
- <a href = "faq.php">FAQ/Help</a>
- <a href = "contact.php">Contact</a>
- <a href = "search.php">Search</a>
- </div>
- <!-- Add a horizontal rule -->
- <hr>
- <!-- The heading -->
- <h2 class = "align-center">Shopping Cart</h2>
- <?php
- $cart = $_SESSION['cart'];
- /* If the cart is not empty */
- if ($cart)
- {
- /* Add the contents of the cart into a variable */
- $items = explode(',',$cart);
- /* Create an array */
- $contents = array();
- /* For each item */
- foreach ($items as $item)
- {
- $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
- //$postage += 20;
- }
- echo('<div id = "cartDiv" class = "align-center">');
- /* Create and display the output */
- echo('<form action="cart.php?option=update" method="post" ID="shoppingcart">');
- echo('<table border = "2">');
- /* output the headings */
- echo('<tr>');
- echo('<td colspan = "2" class="align-center">Product</td>');
- echo('<td>Price</td>');
- echo('<td>QTY</td>');
- echo('<td>Total</td>');
- echo('</tr>');
- /* for each content */
- foreach ($contents as $id=>$qty)
- {
- /* Create the sql */
- $query = "SELECT * FROM Products WHERE ID = ".$id;
- $stmt = OCIParse($connect, $query);
- if(!$stmt)
- {
- echo "An error occurred in parsing the sql string.\n";
- exit;
- }
- OCIExecute($stmt);
- /* While there is still something to fetch */
- while (OCIFetch($stmt))
- {
- /* Store the results in variables */
- $name = OCIResult($stmt, "NAME");
- $price = OCIResult($stmt, "PRICE");
- $id = OCIResult($stmt,"ID");
- }
- /* Continue the output */
- echo('<tr>');
- echo('<td><a href="cart.php?option=delete&ID='.$id.'" style=\'color:black\'>Remove</a></td>');
- echo('<td>'.$name.'</td>');
- echo('<td>$'.$price.'</td>');
- echo('<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>');
- echo('<td>$'.($price * $qty).'</td>');
- $total += $price * $qty;
- echo('</tr>');
- $postage += $qty * 10;
- }
- /*calculate the GST*/
- $gst = $total * .10;
- /* Calculate the final total */
- $grand += $total + $postage + $gst;
- echo('</table>');
- echo('<p>Postage: $'.$postage.'</p>');
- echo('<p>GST: $'.$gst.'</p>');
- echo('<p>Grand Total: <strong>$'.$grand.'</strong></p>');
- echo('<button type="submit">Update Cart</button>');
- echo('<input type = "button" onclick="window.location=\'cart.php?option=empty\'" value ="Empty cart">');
- echo('<input type = "button" onclick="window.location=\'checkout.php\'" value = "Checkout">');
- echo('</form>');
- echo('</div>');
- }
- else
- {
- /* Tell the user the cart is empty */
- echo('<p class="align-center">The shopping cart is empty.</p>');
- }
- /* Disconnects from database */
- OCILogOff($connect);
- echo("<p>"©Deakin University, School of Information Technology. This web page has been developed as a
- student assignment for the unit SIT203: Web Programming. Therefore it is not part of the University's authorised web site.
- DO NOT USE THE INFORMATION CONTAINED ON THIS WEB PAGE IN ANY WAY."</p>");
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement