Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. // Connect to the database
  5. $db_user = 'root'; $db_pass = 'abc123'; $db_uri = "mysql:dbname=rolls;host=127.0.0.1";
  6. $conn = new PDO($db_uri, $db_user, $db_pass);
  7. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  8.  
  9. function add_to_cart($item) {
  10. // check to see if the item is already here if true increment qty
  11. if(isset($_SESSION['cart'])) {
  12. // check for the existence of the item:
  13. $found = false;
  14. $loop = 0;
  15. foreach($_SESSION['cart'] as $item_list) {
  16. if($item_list[0] == $item) {
  17. $_SESSION['cart'][$loop][1]++;
  18. $found = true;
  19. break;
  20. }
  21. $loop++;
  22. }
  23. if($found == false) {
  24. $_SESSION['cart'][] = array($item, 1);
  25. }
  26. } else {
  27. $_SESSION['cart'][] = array($item, 1);
  28. }
  29. }
  30.  
  31. function del_from_cart($item) {
  32. $loop = 0;
  33. $found = false;
  34. foreach($_SESSION['cart'] as $item_list) {
  35. // Search in array for item, then set it's qty to 0;
  36. if($item_list[0] == $item) {
  37. $_SESSION['cart'][$loop][1] = 0;
  38. $found = true;
  39. break;
  40. }
  41. $loop++;
  42. }
  43. }
  44.  
  45. function empty_cart() {
  46. unset($_SESSION['cart']);
  47. }
  48.  
  49. function show_cart() {
  50. // iterate through each item and print it to the screen (SELECT on product table may be necessary)
  51. foreach($_SESSION['cart'] as $item) {
  52. if($item[1] > 0) {
  53. echo $item[0] . ' ' . $item[1];
  54. echo ' <a href="student_cart.php?del_from_cart=' . $item[0] . '">Del</a>';
  55. echo '<br>';
  56. }
  57. }
  58. echo '<p><a href="student_cart.php?empty=true">Empty Cart</a></p>';
  59. }
  60.  
  61. function show_products() {
  62. global $conn;
  63. // SELECT all items available to add to cart and have an 'add to cart' button
  64. $sql = "SELECT * FROM classroom";
  65.  
  66. $check_conn = $conn->prepare($sql);
  67. $check_conn->execute();
  68. $result = $check_conn->fetchAll();
  69.  
  70. foreach($result as $row) {
  71. echo $row['Class_name'] . ' ' . $row['Class_room_no'] . ' ' . $row['Class_day'];
  72. echo ' <a href="student_cart.php?add_to_cart=' . $row['ID'] . '">Add to Cart</a>';
  73. echo '<br>';
  74. }
  75. }
  76.  
  77. if(isset($_GET['del_from_cart'])) {
  78. del_from_cart($_GET['del_from_cart']);
  79. }
  80.  
  81. if(isset($_GET['add_to_cart'])) {
  82. add_to_cart($_GET['add_to_cart']);
  83. }
  84.  
  85. if(isset($_GET['empty'])) {
  86. empty_cart();
  87. }
  88.  
  89. ?>
  90.  
  91. <html>
  92. <script>
  93. </script>
  94. <body>
  95. <?php
  96. show_products();
  97. echo '<hr>';
  98. if(isset($_SESSION['cart'])) {
  99. show_cart();
  100. echo '<hr>';
  101. echo count($_SESSION['cart']);
  102. echo '<br>';
  103. echo print_r($_SESSION['cart']);
  104. }
  105. ?>
  106. </body>
  107. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement