Advertisement
giraffeinatutu

cart 8.22 9p

Aug 22nd, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. <?php
  2. session_start();
  3. $con = mysqli_connect("localhost", "cairil_php", "phpsql");
  4.  
  5. // Check if there were any issues connecting to the database
  6. if ( mysqli_connect_errno() != 0)
  7. {
  8. die("An error occurred trying to establish a connection: " .mysqli_connect_error());
  9. }
  10.  
  11. // Select the database to work with.
  12. $db = mysqli_select_db ($con , "cairil_php");
  13.  
  14. ?>
  15. <html>
  16. <head>
  17. <?php
  18.  
  19. $pid = $_GET[id]; //the product id from the URL
  20. $action = $_GET[action]; //the action from the URL
  21.  
  22.  
  23. switch($action) { //decide what to do
  24.  
  25. case "add":
  26. $_SESSION['cart'][$pid]++;
  27. break;
  28.  
  29. case "remove":
  30. $_SESSION['cart'][$pid]--;
  31. if($_SESSION['cart'][$pid] == 0) unset($_SESSION['cart'][$pid]);
  32. break;
  33.  
  34. case "empty":
  35. unset($_SESSION['cart']);
  36. break;
  37. }
  38.  
  39. ?>
  40. </head>
  41. <title>CR_cart.php</title>
  42. <body>
  43. <center>
  44. <h1>Welcome <?php echo(($_SESSION['fname']) . " " . ($_SESSION['lname'])) ?></h1>
  45. <br>
  46. <h3>Product List</h3>
  47. <br><br>
  48.  
  49.  
  50. <?php
  51.  
  52. if( isset( $_SESSION['cart'])) {
  53.  
  54. echo "<table>"; //format the cart using a HTML table
  55.  
  56. //iterate through the cart, the $product_id is the key and $quantity is the value
  57. foreach($_SESSION['cart'] as $pid => $qty) {
  58.  
  59. $sql = ("SELECT `ProductID`, `ProductName` FROM 'Products' WHERE 'ProductID' = '$pid'");
  60.  
  61. if ($result=mysqli_query($con,$sql))
  62. {
  63.  
  64. // Return the number of rows in result set
  65. $rowcount=mysqli_num_rows($result);
  66.  
  67. if( $rowcount > 0) {
  68.  
  69. list($pid, $pname) = mysql_fetch_row($result);
  70.  
  71.  
  72. echo "<tr>";
  73.  
  74. echo "<td align=\"center\">$name</td>";
  75.  
  76. echo "<td align=\"center\">$qty <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$pid\">X</a></td>";
  77.  
  78. echo "</tr>";
  79.  
  80. }}
  81.  
  82. }
  83.  
  84.  
  85. echo "<tr>";
  86. echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\"><img src=\"/images/b-empty.png\" alt=\"Cart\" onclick=\"return confirm('Are you sure?');\"></a></td>";
  87. echo "</tr>";
  88. echo "</table>";
  89.  
  90.  
  91.  
  92. }else{
  93. //otherwise tell the user they have no items in their cart
  94. echo "Cart Empty.";
  95.  
  96. }
  97.  
  98.  
  99. ?>
  100.  
  101. <br /><br />
  102. <table>
  103. <tr>
  104. <td><a href=CR_products.php><img title"view products" src="/images/b-products.png" alt="View Product" /></a>
  105. <a href=CR_cart.php><img title"view cart" src="/images/b-shopping.png" alt="View Cart" /></a>
  106. <a href=CR_order.php><img title"view order" src="/images/b-orders.png" alt="View Orders"/></a></td></tr>
  107. <tr></tr> <tr></tr>
  108. <tr><td align="center"><a href=CR_welcome.php><img title="home" src="/images/home_button.png" alt="home" width="150" height="50"/></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=CR_logout.php><img title"logout" src="/images/exit_button.jpg" alt="Logout" /></a></td></tr>
  109. </table>
  110. </center>
  111. </body>
  112. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement