Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. <?php
  2. session_start();
  3. include('php/config.php');
  4. $status="";
  5.  
  6. if (isset($_POST['id_producto']) && $_POST['id_producto']!=""){
  7. $id_producto = $_POST['id_producto'];
  8.  
  9. $sql = $mysqli->prepare("SELECT id_producto,producto,precio,imagen FROM productos WHERE id_producto=?");
  10. $sql->bind_param("i",$id_producto);
  11. $sql->execute();
  12. $sql->bind_result($id_producto,$producto,$precio,$imagen);
  13. $sql->fetch();
  14.  
  15. $cartArray = array(
  16. $id_producto=>array(
  17. 'id_producto'=>$id_producto,
  18. 'producto'=>$producto,
  19. 'precio'=>$precio,
  20. 'quantity'=>1,
  21. 'imagen'=>$imagen
  22. )
  23. );
  24.  
  25. if(empty($_SESSION["shopping_cart"])) {
  26. $_SESSION["shopping_cart"] = $cartArray;
  27. $status = "<div class='box'>¡El producto se agrego al carrito!</div>";
  28. } else {
  29. $array_keys = array_keys($_SESSION["shopping_cart"]);
  30. if(in_array($id_producto,$array_keys)) {
  31. $status = "<div class='box' style='color:red;'>¡El producto ya está añadido a su carrito!</div>";
  32. } else {
  33. $_SESSION["shopping_cart"] = array_merge($_SESSION["shopping_cart"],$cartArray);
  34. $status = "<div class='box'>¡El producto se agrego al carrito!</div>";
  35. }
  36. }
  37. }
  38. ?>
  39.  
  40. <?php
  41. $result = $mysqli->prepare("SELECT id_producto,producto,precio,imagen FROM productos");
  42. $result->execute();
  43. $result->store_result();
  44.  
  45. $result->bind_result($id_producto,$producto,$precio,$imagen);
  46. while ($result->fetch()) {
  47. echo "<div class='product_wrapper'>
  48. <form method='post' action=''>
  49. <input type='hidden' name='id_producto' value=".$id_producto." />
  50. <div class='image'><img src='".$imagen."' /></div>
  51. <div class='name'>".$producto."</div>
  52. <div class='price'>$".$precio."</div>
  53. <button type='submit' class='buy'>Buy Now</button>
  54. </form>
  55. </div>";
  56. }
  57. ?>
  58.  
  59. <?php
  60. session_start();
  61. $status="";
  62. if (isset($_POST['action']) && $_POST['action']=="remove"){
  63. if(!empty($_SESSION["shopping_cart"])) {
  64. foreach($_SESSION["shopping_cart"] as $key => $value) {
  65. if($_POST["id_producto"] == $key){
  66. unset($_SESSION["shopping_cart"][$key]);
  67. $status = "<div class='box' style='color:red;'>¡El producto es eliminado de su carrito!</div>";
  68. }
  69. if(empty($_SESSION["shopping_cart"]))
  70. unset($_SESSION["shopping_cart"]);
  71. }
  72. }
  73. }
  74. ?>
  75.  
  76. <?php
  77. foreach ($_SESSION["shopping_cart"] as $producto){
  78. ?>
  79. <tr>
  80. <td><img src='<?php echo $producto['imagen']; ?>' width="50" height="40" /></td>
  81. <td><?php echo $producto['producto']; ?><br>
  82. <form method='post' action=''>
  83. <input type='hidden' name='id_producto' value="<?php echo $producto["id_producto"]; ?>" />
  84. <input type='hidden' name='action' value="remove" />
  85. <button type='submit' class='remove'>Remove Item</button>
  86. </form>
  87. </td>
  88. <td>
  89. <?php echo $producto['quantity']; ?>
  90. </td>
  91. <td><?php echo "$".$producto["precio"]; ?></td>
  92. <td><?php echo "$".$producto["precio"]*$producto["quantity"]; ?></td>
  93. </tr>
  94. <?php
  95. $total_price += ($producto["precio"]*$producto["quantity"]);
  96. }
  97. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement