How to create a Shopping Cart
In this post I’m going to describe how to create a shopping cart . But I am not going to describe each and every thing which must include in shopping cart, but rather give you a hint on what kind of functionality need to have in a good shopping cart ,so you can modify it. Try to develop this shopping cart with pagination and product gallery ,you may get an idea from my previous post.
Step1: create 2 table for product and shopping cart
Step2: display product items with [Add to cart] link or button (Hint: link cart.php?additem=p_id )
Step3: add action to [Add to Cart] link or button with mysql query to add products into shopping cart table
Step4: display shopping cart by the side of products with [remove] link or button ( Hint: link remove.php?remove=p_id )
Step5: add action to [remove] link or button with mysql query to delete products from shopping cart table
Live Demo
Display product items (Product.php)
You can develop this script with adding quantity field and more details link
<body>
<div style=" width:1080px; margin:0 auto;">
<div style="float:left; width:700px;height:100%;padding:20px;"><h2>Items</h2>
<?php
session_start();// start new session
$session=session_id();// get browser unique session identifier (id)
include("db.php");// include database connetion
$query2="select * from shopping";//start display products
$result2=mysql_query($query2);
while($row = mysql_fetch_array($result2)){
$p_id=$row['p_id'];
$p_name=$row['p_name'];
$price=$row['price'];
$details=$row['details'];
echo "ItemName\t $p_name<br/>";
echo "Price\t $price<br/>";
echo "Detail\t $details";
?>
<div style="position:relative;text-align:right;"><!--start add to cart button-->
<form action="cart.php" method="post" name="add">
<input type="hidden" value="<?php echo $p_id;?>" name="additem" />
<input type="submit" value="Add to Cart" />
</form>
</div><!--end add to cart button-->
<hr/>
<?php //end display product
}
?>
</div>
<div style="float:right;width:300px;height:100%;background:#9CF; padding:20px;"><h2>Shopping Cart</h2>
<?php include("show_cart.php");?> <!--include shopping cart -->
</div>
</div>
</body>
Add action to [Add to Cart] link or button (Cart.php)
Try to add this functionality as for a perfect shopping cart. Quantity must update when you adding same product. For that you must check that product already exists or not in your shopping cart.
Hint :
$query1="select quan from shoppingcart where (s_id
='$session' and p_id='$p_id')";
$result1=mysql_query($query1) or die(mysql_error());
if(mysql_num_rows($result1)){
<?php
include("db.php");
session_start();
$session=session_id();//get browser unique session identifier (id)
if(isset($_POST['additem'])){//check if additem variable is set and is not NULL
$p_id=$_POST['additem'];
//You can modify this adding quantity filed with UPDATE mysql command to update info already in a table .
mysql_query("insert into shoppingcart value ('$session','$p_id')");
header("location: home.php");//redirect to home page
}
?>
Display shopping cart(show_cart.php)
You can develop this script with displaying total items,
total price and quantity of each product.
<?php
include("db.php");
$query="SELECT * FROM shopping,shoppingcart WHERE (shopping.p_id = shoppingcart.p_id and shoppingcart.s_id='$session')";
//get related table data where s_id equal to session_id()
$result=mysql_query($query);
while($row = mysql_fetch_array($result)){
$p_id=$row['p_id'];
$p_name=$row['p_name'];
$price=$row['price'];
$quan=$row['quan'];
$details=$row['details'];
// do something ...calculate total amount , total price
echo "ItemName\t $p_id<br/>";
echo "Price\t $price x $quan<br/>";
echo "Detail\t $details";
?>
<div style="position:relative;text-align:right;"><!--start remove button-->
<form action="remove.php" method="post" >
<input type="hidden" value="<?php echo $p_id; ?>" name="removeitem"/>
<input type="submit" onclick="" value="Remove" />
</form><!--end remove button-->
<hr/></div>
<?php
}
?>
<!--do something ...echo total amount , total price ,-->
<input type="button" name="check_out" value="Check Out" style="float:right;"/>
Add action to [remove] link or button (remove.php)
<?php
include("db.php");
session_start();
$session=session_id();
if(isset($_POST['removeitem'])){
$p_id = $_POST['removeitem'];
$query="DELETE from shoppingcart where (s_id ='$session' and p_id='$p_id') ";
//Removing Product from Shopping Cart
$result=mysql_query($query);
header("location: home.php");
}
?>
If you like this post, please share it with others.

