Advertisement
Guest User

Untitled

a guest
Sep 27th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. <?php
  2. session_start();
  3. $host = 'localhost';
  4. $user = 'root';
  5. $pass = '';
  6. $db = 'fsc_management_system';
  7.  
  8. $con = mysqli_connect($host, $user ,$pass, $db);
  9. /*if($con)
  10. {
  11. echo "Connected";
  12. }*/
  13. if(mysqli_connect_errno())
  14. {
  15. echo 'Database connection failed with the following errors: '. mysqli_connect_error();
  16. die();
  17. }
  18. if(isset($_POST["add_to_cart"]))
  19. {
  20. if(isset($_SESSION["shopping_cart"]))
  21. {
  22. $item_array_id = array_column($_SESSION["shopping_cart"], "item_id");
  23. if(!in_array($_GET["id"], $item_array_id))
  24. {
  25. $count = count($_SESSION["shopping_cart"]);
  26. $item_array = array(
  27. 'item_id' => $_GET["id"],
  28. 'item_name' => $_POST["hidden_name"],
  29. 'item_price' => $_POST["hidden_price"],
  30. 'item_quantity' => $_POST["quantity"]
  31. );
  32. $_SESSION["shopping_cart"][$count] = $item_array;
  33. }
  34. else
  35. {
  36. echo '<script>alert("Item Already Added")</script>';
  37. echo '<script>window.location="ecommerce.php"</script>';
  38. }
  39. }
  40. else
  41. {
  42. $item_array = array(
  43. 'item_id' => $_GET["id"],
  44. 'item_name' => $_POST["hidden_name"],
  45. 'item_price' => $_POST["hidden_price"],
  46. 'item_quantity' => $_POST["quantity"]
  47. );
  48. $_SESSION["shopping_cart"][0] = $item_array;
  49. }
  50. }
  51. if(isset($_GET["action"]))
  52. {
  53. if($_GET["action"] == "delete")
  54. {
  55. foreach($_SESSION["shopping_cart"] as $keys => $values)
  56. {
  57. if($values["item_id"] == $_GET["id"])
  58. {
  59. unset($_SESSION["shopping_cart"][$keys]);
  60. echo '<script>alert("Item Removed")</script>';
  61. echo '<script>window.location="ecommerce.php"</script>';
  62. }
  63. }
  64. }
  65. }
  66. ?>
  67. <!DOCTYPE html>
  68. <html>
  69. <head>
  70. <title>FSC Shopping Cart</title>
  71. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  72. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  73. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  74. </head>
  75. <style>
  76. .img-responsive
  77. {
  78. width: auto;
  79. height: 150px;
  80. }
  81.  
  82.  
  83. </style>
  84. <body>
  85. <br />
  86. <div class="container" style="width:700px;">
  87. <h3 align="center">FSC Shopping Cart</h3><br />
  88. <?php
  89. $query = "SELECT * FROM shopping_cart ORDER BY id ASC";
  90. $result = mysqli_query($con, $query);
  91. if(mysqli_num_rows($result) > 0)
  92. {
  93. while($row = mysqli_fetch_array($result))
  94. {
  95. ?>
  96. <div class="col-md-4">
  97. <form method="post" action="ecommerce.php?action=add&id=<?php echo $row["id"]; ?>">
  98. <div style="border:1px solid #333; background-color:#f1f1f1; border-radius:5px; padding:16px;" align="center">
  99. <img src="<?php echo $row["image"]; ?>" class="img-responsive" /><br />
  100. <h4 class="text-info"><?php echo $row["name"]; ?></h4>
  101. <h4 class="text-danger">$ <?php echo $row["price"]; ?></h4>
  102. <input type="text" name="quantity" class="form-control" value="1" />
  103. <input type="hidden" name="hidden_name" value="<?php echo $row["name"]; ?>" />
  104. <input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>" />
  105. <input type="submit" name="add_to_cart" style="margin-top:5px;" class="btn btn-success" value="Add to Cart" />
  106. </div>
  107. </form>
  108. </div>
  109. <?php
  110. }
  111. }
  112. ?>
  113. <div style="clear:both"></div>
  114. <br />
  115. <h3>Order Details</h3>
  116. <div class="table-responsive">
  117. <table class="table table-bordered">
  118. <tr>
  119. <th width="40%">Item Name</th>
  120. <th width="10%">Quantity</th>
  121. <th width="20%">Price</th>
  122. <th width="15%">Total</th>
  123. <th width="5%">Action</th>
  124. </tr>
  125. <?php
  126. if(!empty($_SESSION["shopping_cart"]))
  127. {
  128. $total = 0;
  129. foreach($_SESSION["shopping_cart"] as $keys => $values)
  130. {
  131. ?>
  132. <tr>
  133. <td><?php echo $values["item_name"]; ?></td>
  134. <td><?php echo $values["item_quantity"]; ?></td>
  135. <td>$ <?php echo $values["item_price"]; ?></td>
  136. <td>$ <?php echo number_format($values["item_quantity"] * $values["item_price"], 2); ?></td>
  137. <td><a href="ecommerce.php?action=delete&id=<?php echo $values["item_id"]; ?>"><span class="text-danger">Remove</span></a></td>
  138. </tr>
  139. <?php
  140. $total = $total + ($values["item_quantity"] * $values["item_price"]);
  141. }
  142. ?>
  143. <tr>
  144. <td colspan="3" align="right">Total</td>
  145. <td align="right">$ <?php echo number_format($total, 2); ?></td>
  146. <td></td>
  147. </tr>
  148. <?php
  149. }
  150. ?>
  151. </table>
  152. </div>
  153. </div>
  154. <br />
  155. </body>
  156. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement