Advertisement
Guest User

Untitled

a guest
Jun 18th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.45 KB | None | 0 0
  1. <?php
  2.  
  3. /* Start the session */
  4. session_start();
  5.  
  6. /* Connect to the Database */
  7. $dbuser = "dsar";
  8. $dbpass = "(W6rIXNF";
  9. $db = "SSID";
  10.  
  11. $connect = OCILogon($dbuser, $dbpass, $db);
  12.  
  13. /* Process the actions */
  14. $cart = $_SESSION['cart'];
  15. $option = $_GET['option'];
  16.  
  17. /* Perform the appropiate action based on the user's selection */
  18. switch($option)
  19. {
  20. /* Add to the cart */
  21. case 'add':
  22. /* If there is something in the cart */
  23. if ($cart)
  24. {
  25. /* Add the item to the end of the cart */
  26. $cart .= ','.$_GET['ID'];
  27. /* Calculate the postage */
  28. }
  29. else
  30. {
  31. /* Add the item to the cart */
  32. $cart = $_GET['ID'];
  33. }
  34. /* Add the cart to the session variable */
  35. $_SESSION['cart'] = $cart;
  36. break;
  37. /* Delete one item from the cart */
  38. case 'delete':
  39. /* If the cart exists */
  40. if ($cart)
  41. {
  42. /* Create an array */
  43. $items = explode(',',$cart);
  44. /* Create a new cart */
  45. $newcart = '';
  46. /* For each item */
  47. foreach($items as $item)
  48. {
  49. /* If the item is not equal to the one we want to remove */
  50. if ($_GET['ID']!= $item)
  51. {
  52. /* If the new cart isn't empty */
  53. if ($newcart != '')
  54. {
  55. /* Add the item */
  56. $newcart .= ','.$item;
  57. }
  58. else
  59. {
  60. /* Add the item to the cart */
  61. $newcart = $item;
  62. }
  63. }
  64. }
  65.  
  66. /* Add the new cart back into the original cart */
  67. $cart = $newcart;
  68.  
  69. }
  70. /* Add the cart to the session variable */
  71. $_SESSION['cart'] = $cart;
  72. break;
  73. case 'update':
  74. /* If the cart exists */
  75. if ($cart)
  76. {
  77. /* declare a new cart */
  78. $newcart = '';
  79.  
  80. /* For each value */
  81. foreach($_POST as $key=>$value)
  82. {
  83. /* If the string is in the right format */
  84. if (stristr($key,'qty'))
  85. {
  86. /* Remove the qty from the string and store it in a variable */
  87. $id = str_replace('qty','',$key);
  88.  
  89. /* Add the items to a new variable */
  90. $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
  91.  
  92. /* empty the new cart */
  93. $newcart = '';
  94.  
  95. /* for each item */
  96. foreach ($items as $item)
  97. {
  98. /* if the id is not equal to the item */
  99. if ($id != $item)
  100. {
  101. /* If the new cart is not empty */
  102. if ($newcart != '')
  103. {
  104. /* Add the item to the cart */
  105. $newcart .= ','.$item;
  106. }
  107. else
  108. {
  109. /* Add the item to the cart */
  110. $newcart = $item;
  111. }
  112. }
  113. }
  114. /* for each value */
  115. for($i = 1; $i <= $value; $i++)
  116. {
  117. /* If the new cart is not null */
  118. if ($newcart != '')
  119. {
  120. /* Add the id to the cart */
  121. $newcart .= ','.$id;
  122. }
  123. else
  124. {
  125. /* Add the id to the cart */
  126. $newcart = $id;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. /* Copy the new cart back into the original */
  133. $cart = $newcart;
  134. /* Add the cart to the session variable */
  135. $_SESSION['cart'] = $cart;
  136. break;
  137. case 'empty':
  138. /* delete the session variable */
  139. unset($_SESSION['cart']);
  140. break;
  141. }
  142. ?>
  143.  
  144. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  145. <html>
  146. <head>
  147. <title>Shopping Cart</title>
  148. <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
  149. <link rel="stylesheet" type="text/css" href="format.css" >
  150. <!-- CSS to control the color of the links -->
  151. <style type = "text/css">
  152. A:link {text-decoration: underline; color: white;}
  153. A:visited {text-decoration: underline; color: black}
  154. A:hover {text-decoration: underline; color: red;}
  155. </style>
  156. </head>
  157.  
  158. <body class ="background-main">
  159. <div id = "linkDiv" class = "align-center spacing background-links">
  160. <!-- The heading of the website -->
  161. <h1 style = "word-spacing:0px">Disarm Anime Warehouse</h1>
  162. <!-- A list of links to the other pages -->
  163. <a href = "ass2.php">Home</a>
  164. <a href = "information.php">Information</a>
  165. <a href = "product.php">Products</a>
  166. <a href = "faq.php">FAQ/Help</a>
  167. <a href = "contact.php">Contact</a>
  168. <a href = "search.php">Search</a>
  169. </div>
  170.  
  171. <!-- Add a horizontal rule -->
  172. <hr>
  173.  
  174. <!-- The heading -->
  175. <h2 class = "align-center">Shopping Cart</h2>
  176.  
  177. <?php
  178. $cart = $_SESSION['cart'];
  179. /* If the cart is not empty */
  180. if ($cart)
  181. {
  182. /* Add the contents of the cart into a variable */
  183. $items = explode(',',$cart);
  184.  
  185. /* Create an array */
  186. $contents = array();
  187.  
  188. /* For each item */
  189. foreach ($items as $item)
  190. {
  191. $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
  192. //$postage += 20;
  193.  
  194. }
  195.  
  196. echo('<div id = "cartDiv" class = "align-center">');
  197.  
  198. /* Create and display the output */
  199. echo('<form action="cart.php?option=update" method="post" ID="shoppingcart">');
  200. echo('<table border = "2">');
  201.  
  202. /* output the headings */
  203. echo('<tr>');
  204. echo('<td colspan = "2" class="align-center">Product</td>');
  205. echo('<td>Price</td>');
  206. echo('<td>QTY</td>');
  207. echo('<td>Total</td>');
  208. echo('</tr>');
  209.  
  210. /* for each content */
  211. foreach ($contents as $id=>$qty)
  212. {
  213. /* Create the sql */
  214. $query = "SELECT * FROM Products WHERE ID = ".$id;
  215.  
  216. $stmt = OCIParse($connect, $query);
  217.  
  218. if(!$stmt)
  219. {
  220. echo "An error occurred in parsing the sql string.\n";
  221. exit;
  222. }
  223. OCIExecute($stmt);
  224.  
  225. /* While there is still something to fetch */
  226. while (OCIFetch($stmt))
  227. {
  228. /* Store the results in variables */
  229. $name = OCIResult($stmt, "NAME");
  230. $price = OCIResult($stmt, "PRICE");
  231. $id = OCIResult($stmt,"ID");
  232. }
  233.  
  234. /* Continue the output */
  235. echo('<tr>');
  236. echo('<td><a href="cart.php?option=delete&ID='.$id.'" style=\'color:black\'>Remove</a></td>');
  237. echo('<td>'.$name.'</td>');
  238. echo('<td>$'.$price.'</td>');
  239. echo('<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>');
  240. echo('<td>$'.($price * $qty).'</td>');
  241. $total += $price * $qty;
  242. echo('</tr>');
  243.  
  244. $postage += $qty * 10;
  245.  
  246. }
  247.  
  248. /*calculate the GST*/
  249. $gst = $total * .10;
  250.  
  251.  
  252. /* Calculate the final total */
  253. $grand += $total + $postage + $gst;
  254.  
  255. echo('</table>');
  256. echo('<p>Postage: $'.$postage.'</p>');
  257. echo('<p>GST: $'.$gst.'</p>');
  258. echo('<p>Grand Total: <strong>$'.$grand.'</strong></p>');
  259. echo('<button type="submit">Update Cart</button>');
  260. echo('<input type = "button" onclick="window.location=\'cart.php?option=empty\'" value ="Empty cart">');
  261. echo('<input type = "button" onclick="window.location=\'checkout.php\'" value = "Checkout">');
  262. echo('</form>');
  263. echo('</div>');
  264. }
  265. else
  266. {
  267. /* Tell the user the cart is empty */
  268. echo('<p class="align-center">The shopping cart is empty.</p>');
  269. }
  270.  
  271. /* Disconnects from database */
  272. OCILogOff($connect);
  273. echo("<p>"&copy;Deakin University, School of Information Technology. This web page has been developed as a
  274. student assignment for the unit SIT203: Web Programming. Therefore it is not part of the University's authorised web site.
  275. DO NOT USE THE INFORMATION CONTAINED ON THIS WEB PAGE IN ANY WAY."</p>");
  276.  
  277. ?>
  278. </body>
  279. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement