How To Create Shoping Cart in php
1 create a Db and create a table like movie and fields are id, name, description image, price etc and insert data.
2 Create web page for show your products from db.
3 provide a hiper link on your product image or provide botton the code is
<a href="products.php?id=<?php echo $id; ?>
and redirect after clicking on products.php page and also send product id with this code
?id=<?php echo $id;
4 then we go to products.php page and we get product id in url just like below.
http://localhost/movies/products.php?id=1
we create a table for fetch all details from db like name, price and provide a select box in form and form send to addtocart.php page after click on add to cart botton justlike below.
// Code of product page //
<table width="100%" >
<tr>
<td><h1>Movie Name </h1></td>
<td><h1>Movie Price</h1> </td>
<td><h1>Total Seats </h1> </td>
<td><h1>Action </h1></td>
</tr>
<tr>
<?php
$sql="select * from movie where id = '".$_GET['id']."'";
$row=mysql_query($sql);
while($data=mysql_fetch_array($row))
{
$price=$data['price'];
$name=$data['name'];
$id=$data['id'];
//$_SESSION['price']=$price;
//$_SESSION['value']=$name;
?>
<td> <?php echo $name; ?></td>
<td><?php echo $price; ?> </td>
<?php } ?>
<td> <FORM method="post" action="addtocart.php?id=<?php echo $id; ?>">
<select name="select" id="select">
<option value="1">1</option>
<option value=" 2"> 2</option>
<option value=" 3"> 3</option>
</select>
</td>
<td>
<input type="submit" name="submit" id="submit" value="Add To Cart" >
</td>
</tr>
</form>
// Then We Go To Add To Cart page //
1 we recieved product id from products.php page to add to cart page just like below.
http://localhost/movies/addtocart.php?id=1
2 We Create a table for fetch data from db with the help of product id.
3 select product details show like name, price, quentity, total amount.
4 and we count total amount from formula (A=A+B) and save in session varible.
5 Print the total cart value onevery web page from session varible.
// Code of add to cart page //
<html>
<body>
<h1> Thanks for online booking with sinemax.your Details are below. </h1>
<table align="center" width="100%" border="1" >
<tr>
<td><h1>Movie Name </h1></td>
<td><h1>Movie Price</h1> </td>
<td><h1>Total Seats </h1> </td>
<td><h1>Total Amount </h1></td>
</tr>
<tr>
<?php
include"connection.php";
session_start();
$sql="select * from movie where id ='".$_GET['id']."'";
$row=mysql_query($sql);
while($data=mysql_fetch_array($row))
{
$nam=$data['name'];
$pri=$data['price'];
}
?>
<td><h3><?php echo $nam; ?> </h3></td>
<td><h3><?php echo $pri; ?></h3> </td>
<td><h3><?php echo $per= $_POST['select']; ?> </h3> </td>
<td><h3><?php echo $total= $pri*$per;?></h3></td>
<?php echo $_SESSION['amount']=$_SESSION['amount']+$total;
?>
</tr>
</table>
</body>
</html>
// print the session varible on every page //
session_start();
echo $_SESSION['amount'];
// Show Cart Products in array(2D) on addtocart.php page //
1 We send product id on add to cart page from previous page (Product.php)
2 We fetch product name, price, from db.
3 store products details in varible.($nam, $pri, $per, $total)
4 and all varible stored in array just like below
$a=array($nam, $pri, $per, $total);
5 create session varible and store 2d array and assign value of $a (Array) just like below.
$_SESSION['cart'][]=$a;
6 Tjen we count session varible from count function just like below.
count($_SESSION['cart']);
7 Then We print session varible in for lopp just like below.
for($i=0; $i<count($_SESSION['cart']); $i++)
8 we print session varible in td to print products just like below.
<td><h3><?php echo $_SESSION['cart'][$i][0]; ?> </h3></td>
<td><h3><?php echo $_SESSION['cart'][$i][1]; ?></h3> </td>
<td><h3><?php echo$_SESSION['cart'][$i][2]; ?> </h3> </td>
<td><h3><?php echo $_SESSION['cart'][$i][3]; ?></h3></td>
8 At the last point of table we calculate all amount just like below.
<?php echo $_SESSION['amount']=$_SESSION['amount']+$total;?>
// Sourse code for display products (aatocart.php) //
<html>
<body>
<h1> Thanks for online booking with sinemax.your Details are below. </h1>
<table align="center" width="90%" border="1" >
<tr>
<td><h2>Movie Name </h2></td>
<td><h2>Movie Price</h2> </td>
<td><h2>Total Seats </h2> </td>
<td><h2>Total Amount </h2></td>
<td><h2>Action </h2></td>
</tr>
<?php
include"connection.php";
session_start();
//echo '<pre>';
//print_r($_SESSION['cart']);
$sql="select * from movie where id ='".$_GET['id']."'";
$row=mysql_query($sql);
while($data=mysql_fetch_array($row))
{
$id =$data['id'];
$nam=$data['name']; echo '<br>';
$pri=$data['price'];
$per= $_POST['select'];
$total= $pri*$per;
$a=array($nam, $pri, $per, $total);
$_SESSION['cart'][]=$a;
echo count($_SESSION['cart']);
}
?>
<?php $gtotal=0;
for($i=0; $i<count($_SESSION['cart']); $i++)
{
?>
<tr>
<td><h3><?php echo $_SESSION['cart'][$i][0]; ?> </h3></td>
<td><h3><?php echo $_SESSION['cart'][$i][1]; ?></h3> </td>
<td><h3><?php echo$_SESSION['cart'][$i][2]; ?> </h3> </td>
<td><h3><?php echo $_SESSION['cart'][$i][3]; ?></h3></td>
<?php echo $gtotal=$gtotal+$_SESSION['cart'][$i][3]; ?>
<td><h3><a href="remove.php?id=<?php echo $i; ?>"><input type="submit" name="submit" id="submit" value="Remove" ></h3></a></td>
</tr>
<?php } ?>
<tr>
<td>Total Amount</td>
<td></td>
<td></td>
<td>
<?php echo $gtotal;
?></td>
</tr>
</table>
</body>
</html>
// Remove selected products from remove.php //
1 we create new page remove.php
2 send array number ($i) value on remove.php page from previous page addtocart.php page like below.
<a href="remove.php?id=<?php echo $i; ?>"><input type="submit" name="submit" id="submit" value="Remove" ></h3></a>
3 we get selected product array id on page just like below.
http://localhost/movies/remove.php?id=0
4 then we get array number from url just like below.
$j=$_GET['id'];
5 and then unset session varible just like below
unset($_SESSION['cart'][$j]);
6 then set array in propr way so blank row not show just like below.
$_SESSION['cart']= array_values($_SESSION['cart']);
// Code for remove products. //
<?php
session_start();
echo $j=$_GET['id'];
echo $j;
unset($_SESSION['cart'][$j]);
$_SESSION['cart']= array_values($_SESSION['cart']);
echo '<pre>';
print_r($_SESSION['cart']);
//header("location:addtocart.php?$j");
?>
Comments
Post a Comment