Learn the Basics of Two dimensional array shopping cart scripts
This script uses three main parts .How to display images by taking data from MySQL table.
$qty=1; // default value of quantity set to 1
if(isset($_SESSION[cart])){
foreach ($_SESSION['cart'] as $key => $val) {
if($row[p_id]=== $_SESSION['cart'][$key]['p_id']){
$qty=$_SESSION['cart'][$key]['qty'];
break;
}
}
}
The variable $qty we will use to set the default value of each record ( Product ) while displaying inside while loop.
echo "<table class='table my_table'>
<tr class='info'><th>Picture</th> <th> ID</th><th>Name</th><th>Quantity</th><th>Unit Price</th><th>Add</th><th>Remove</th></tr>";
while ($row = $stmt->fetch_assoc()) {
$qty=1; // default value of quantity set to 1
if(isset($_SESSION[cart])){
foreach ($_SESSION['cart'] as $key => $val) {
if($row[p_id]=== $_SESSION['cart'][$key]['p_id']){
$qty=$_SESSION['cart'][$key]['qty'];
break;
}
}
}
echo "<tr><td ><img src=images/$row[img] class=rounded></td><td>$row[p_id]</td><td >$row[p_name]</td><td><input class='selector' id=$row[p_id]_qty size=2 value='$qty' data-sp_id=$row[p_id]></code> </td><td >$row[price] </td><td ><img src=cart-add.jpg class='img-thumbnail cart-add' id=$row[p_id]> </td><td><img src=cart-remove.jpg class='img-thumbnail cart-remove' data-remove_id=$row[p_id]> </td></tr>";
}
echo "</table>";
After changing the quantity, the product will automatically added to the cart along with the quantity ordered ( set by user through spinner ) . By making the quantity equal to 0 , user can remove the product from shopping cart.
$('.cart-add').click(function(){
var id=$(this).attr('id');
var qty=$('#'+id+'_qty').val();
my_function(id,qty); // Post data using Function
})
By changing the quantity cart will be update with new data. Product will be removed from the cart by making quantity equal to zero.
$( ".selector" ).spinner({
max: 10,
min: 0,
stop: _.debounce(function(e, ui) {
var id=$(this).data('sp_id');
var qty=$('#'+id+'_qty').val();
my_function(id,qty);
}, 500)
});
We used debounce function to introduce some delay while changing the value of the spinner. var debouncedStop = _.debounce(function(e, ui) {
show_error();
}, 10000);
By setting the spinner (quantity ) to 0 , the product can be removed from the shopping cart.
$('.cart-remove').click(function(){// remove button is clicked.
my_function($(this).data('remove_id'),0);//Post data
$('#'+$(this).data('remove_id')+'_qty').spinner("value",0);
})
In the above code we are sending product id and quantity = 0 to backend script. After posting we are setting the corresponding spinner value to 0
my_function=function my_function(id,qty){
$.post('add.php',{'p_id':id,'qty':qty},function(return_data){
$("#msg_display").html(return_data.msg);
$("#size_of_cart").html(return_data.size_of_cart);
$("#msg_display").show();
setTimeout(function() { $("#msg_display").fadeOut('slow'); }, 4000);
},"json");
}
<button type='button' class='btn btn-primary'>
Number of Items in the cart = <span id=size_of_cart class='badge badge-light'>".sizeof($_SESSION['cart']). "</span>
</button>
After every update of the cart we will count the total number of elements ( products ) present and accordingly change the value using JQuery.
$("#size_of_cart").html(return_data.size_of_cart);
In above code size_of_cart
is returned by our backend script add.php file with a value equal to number of elements present inside our $_SESSION[cart]
array.
12-06-2020 | |
Very helpful. Thanks. |
08-09-2020 | |
Great article and examples! It saved a lot of time. It is just what I needed. |
13-02-2021 | |
Very good.Thank you very much. |