Search This Blog

27 July 2018

PHP using Shopping Cart

PHP Using Shopping Cart


Index.php:

  <!DOCTYPE html>
<html>
<head>
<style>
.overlay {
  background: #304352;
  opacity: 0.4;
  position: absolute;
 right: 0;
}
.overlay1 {
  background: #fff;
  opacity: 0.4;
  position: absolute;

  top: 0;
  left: 0;
  right: 0;

}
</style>
<title> Admin Panel </title>
</head>
<script src="js/jquery.js"> </script>
<link rel="stylesheet" type="text/css" href=" css/bootstrap.min.css">
<body>
<div class="container">
<div class="row">
<center> <h1> Shopping Cart </h1> </center>
<div class="col-md-12">
<ul type="none">
<?php $con=mysqli_connect("localhost","root","","test");
   $sql=" select * from product ";
   $res=$con->query($sql);
   while($row= mysqli_fetch_array($res)){
?>
<li >
<div class="col-md-3" >
<img width="100%" src="images/<?php echo $row['image']; ?>" >
<center>
<h4> <?php echo $row['pname']; ?> </h4>
<h4> <?php echo $row['pdesc']; ?> </h4>
<h4> <?php echo "$".$row['price']; ?> </h4>
<a href="index.php?id=<?php echo $row['id']; ?>" class="btn btn-danger"> Add cart </a>
</center>
</div>
</li><?php } ?>
</ul><br>
<h4><a href="show.php" target="_blank">Show cart</a></h4>
</div>
</div>
</div>
</body>
</html>
<?php
session_start();

if(empty($_SESSION['cart']))
$_SESSION['cart']=array();
if(isset($_GET['id'])){
array_push($_SESSION['cart'],@$_GET['id']);
}
?>

Show Product:
<?php
$con=mysqli_connect("localhost","root","","test");

if(!isset($_SESSION['cart']) or $_SESSION['cart']==NULL)
echo " No product";
else{
$id=implode(',',$_SESSION['cart']);
$sql=" select * from product where id in($id) ";
$res=$con->query($sql);
echo "<table class='table table-hover'><tr>
<th> Id </th> <th> name </th> <th> Description </th><th> Image </th> <th> Quantity </th> <th> price </th>
</tr>";
$price=0;
while($row=mysqli_fetch_array($res)){
$price+=$row['price']*$row['quantity'];
echo "

<tr>
<td> ".$row['id']." </td>
<td> ".$row['pname']." </td>
<td> ".$row['pdesc']." </td>
<td> <img  width='18%' src='images/".$row['image']."' </td>
<td> <input style='width:50px;' value='".$row['quantity']."' type='text' onkeyup='inc(".$row['id'].")' name='qty[]' id='qty".$row['id']."'></td>
<td id='s".$row['id']."'>".$row['price']*$row['quantity']." </td>
<td> <a href='?rm=".$row['id']."'> Remove </a> </td>
</tr>
";



}echo " <tr>
<td colspan='5'>
Total
</td>
<td> ".$price." </td>
</tr>

</table> <br>";

echo " <h4> <a href='?all'> remove all </a> </h4>";

}

Remove Product :


<?php     //Remove All
session_start();
if(isset($_GET['all'])){
unset($_SESSION['cart']);
}


else if (isset($_GET['rm'])){  //Remove Particular cart
foreach ($_SESSION['cart'] as $key => $pro) {
if($pro==$_GET['rm']){
unset($_SESSION['cart'][$key]);
}

}
}
?>

Increase Quantity:

<script type="text/javascript">
function inc(e){
var qty=document.getElementById('qty'+e).value;
$.post("inc.php",{id:e,qty:qty},function(data){
document.getElementById('s'+e).innerHTML=data;
});
}

</script>


<?php
session_start();
$con=mysqli_connect("localhost","root","","test");
if(isset($_POST['id'])){
$id=$_POST['id'];
$qty=$_POST['qty'];
$sql=" update product set quantity='$qty' where id='$id' ";
$con->query($sql);
$sql=" select * from product where id='$id' ";
$res=$con->query($sql);
while($row=mysqli_fetch_array($res)){
$price=$row['quantity']*$row['price'];
echo $_SESSION['price']=$price;
}
}

?>

# 3 Files 

1.index.php
2.show.php
3.inc.php


Output:
















2 comments:

  1. Thanks for sharing the useful Code Snippet on increasing the quantity of the Product,showing and removing the Products.

    Web Application Development Company in Coimbatore

    ReplyDelete
  2. Nice and informative article hope your will share more article, Onajah is a best Online video tutorial platform in Pakistan for more information visit this.

    ReplyDelete

Jquery or Javascript Start Exam Time

 <script> function startTimer() {      var date = "<?php echo $date ?>"; // dynamic date      var time = "<?...