· 8 years ago · Apr 10, 2018, 04:54 PM
1<?php
2session_start();
3$product_ids = array();
4//session_destroy();
5
6//check if Add to Cart button has been submitted
7if(filter_input(INPUT_POST, 'add_to_cart')){
8 if(isset($_SESSION['shopping_cart'])){
9
10 //keep track of how mnay products are in the shopping cart
11 $count = count($_SESSION['shopping_cart']);
12
13 //create sequantial array for matching array keys to products id's
14 $product_ids = array_column($_SESSION['shopping_cart'], 'id');
15
16 if (!in_array(filter_input(INPUT_GET, 'id'), $product_ids)){
17 $_SESSION['shopping_cart'][$count] = array
18 (
19 'id' => filter_input(INPUT_GET, 'id'),
20 'name' => filter_input(INPUT_POST, 'name'),
21 'price' => filter_input(INPUT_POST, 'price'),
22 'quantity' => filter_input(INPUT_POST, 'quantity')
23 );
24 }
25 else { //product already exists, increase quantity
26 //match array key to id of the product being added to the cart
27 for ($i = 0; $i < count($product_ids); $i++){
28 if ($product_ids[$i] == filter_input(INPUT_GET, 'id')){
29 //add item quantity to the existing product in the array
30 $_SESSION['shopping_cart'][$i]['quantity'] += filter_input(INPUT_POST, 'quantity');
31 }
32 }
33 }
34
35 }
36 else { //if shopping cart doesn't exist, create first product with array key 0
37 //create array using submitted form data, start from key 0 and fill it with values
38 $_SESSION['shopping_cart'][0] = array
39 (
40 'id' => filter_input(INPUT_GET, 'id'),
41 'name' => filter_input(INPUT_POST, 'name'),
42 'price' => filter_input(INPUT_POST, 'price'),
43 'quantity' => filter_input(INPUT_POST, 'quantity')
44 );
45 }
46}
47
48if(filter_input(INPUT_GET, 'action') == 'delete'){
49 //loop through all products in the shopping cart until it matches with GET id variable
50 foreach($_SESSION['shopping_cart'] as $key => $product){
51 if ($product['id'] == filter_input(INPUT_GET, 'id')){
52 //remove product from the shopping cart when it matches with the GET id
53 unset($_SESSION['shopping_cart'][$key]);
54 }
55 }
56 //reset session array keys so they match with $product_ids numeric array
57 $_SESSION['shopping_cart'] = array_values($_SESSION['shopping_cart']);
58} ?>
59
60<div class="container">
61 <?php
62
63 $connect = mysqli_connect('localhost', 'root', 'mypass123', 'cart');
64 $query = 'SELECT * FROM products ORDER by id ASC';
65 $result = mysqli_query($connect, $query);
66
67 if ($result):
68 if(mysqli_num_rows($result)>0):
69 while($product = mysqli_fetch_assoc($result)):
70 //print_r($product);
71 ?>
72 <div class="col-sm-4 col-md-3" >
73 <form method="post" action="cart.php?action=add&id=<?php echo $product['id']; ?>">
74 <div class="products">
75 <img src="<?php echo $product['image']; ?>" class="img-responsive" />
76 <h4 class="text-info"><?php echo $product['name']; ?></h4>
77 <h4>$ <?php echo $product['price']; ?></h4>
78 <input type="text" name="quantity" class="form-control" value="1" />
79 <input type="hidden" name="name" value="<?php echo $product['name']; ?>" />
80 <input type="hidden" name="price" value="<?php echo $product['price']; ?>" />
81 <input type="submit" name="add_to_cart" style="margin-top:5px;" class="btn btn-info"
82 value="Add to Cart" />
83 </div>
84 </form>
85 </div>
86 <?php
87 endwhile;
88 endif;
89 endif;
90 ?>
91
92<div class="table-responsive">
93 <table class="table">
94 <tr><th colspan="5"><h3>Order Details</h3></th></tr>
95 <tr>
96 <th width="40%">Product Name</th>
97 <th width="10%">Quantity</th>
98 <th width="20%">Price</th>
99 <th width="15%">Total</th>
100 <th width="5%">Action</th>
101 </tr>
102 <?php
103 if(!empty($_SESSION['shopping_cart'])):
104
105 $total = 0;
106
107 foreach($_SESSION['shopping_cart'] as $key => $product):
108 ?>
109 <tr>
110 <td><?php echo $product['name']; ?></td>
111 <td><?php echo $product['quantity']; ?></td>
112 <td>$ <?php echo $product['price']; ?></td>
113 <td>$ <?php echo number_format($product['quantity'] * $product['price'], 2); ?></td>
114 <td>
115 <a href="cart.php?action=delete&id=<?php echo $product['id']; ?>">
116 <div class="btn-danger">Remove</div>
117 </a>
118 </td>
119 </tr>
120 <?php
121 $total = $total + ($product['quantity'] * $product['price']);
122 endforeach;
123 ?>
124 <tr>
125 <td colspan="3" align="right">Total</td>
126 <td align="right">$ <?php echo number_format($total, 2); ?></td>
127 <td></td>
128 </tr>
129 <tr>
130 <!-- Show checkout button only if the shopping cart is not empty -->
131 <td colspan="5">
132 <?php
133 if (isset($_SESSION['shopping_cart'])):
134 if (count($_SESSION['shopping_cart']) > 0):
135 ?>
136 <a href="#" class="button">Checkout</a>
137 <?php endif; endif; ?>
138 </td>
139 </tr>
140 <?php
141 endif;
142 ?>
143 </table>
144 </div>
145
146$(".product-form").submit(function(e){
147 var form_data = $(this).serialize();
148 var button_content = $(this).find('button[type=submit]');
149 button_content.html('Adding...');
150 $.ajax({
151 url:"display_product.php",
152 type:"POST",
153 dataType:"json",
154 data: form_data
155 }).done(function(data){
156 $("#title-cartList").html(data.shopping_cart);
157 button_content.html('Add to Cart');
158 })
159 e.preventDefault();
160 });
161
162 $(document).read(function(){
163 $('#addToCart').click(function(){
164 var id = $('#id').val();
165 var cakename = $('#cakename').val();
166 var price = $('#price').val();
167 var quantity = $('#quantity').val();
168
169 // alert(id + " "+cakename + " "+ price +" "+ quantity);
170 $.ajax({
171 url:'display_product.php',
172 type: 'POST',
173 data:{
174 id:id,
175 cakename:cakename,
176 price:price,
177 quantity:quantity
178 }
179 }).done(function(data){
180 $('.title-cartList').html(data)
181 }).fail(function(data){
182
183 });
184 });
185 });