Blog Introductions

In this  blog contains tutorials about  HTML, Python, CSS, How to, PHP Tutorials, Java, C++, Tutorials,  Examples, Source code,Learning,

Tips and Software development  services.Shopping cart system with php mysql. 


Shopping cart system with php and mysql - Php tutorials  

Bestitworriors

Shopping cart system with php and mysql
Shopping cart system with php and mysql



Cart Basic code flow with php


PHP Tutorials - Here i will let you know about php Shopping cart system with php.Its very easy and simple to implement the php shoping cart system in our website . First i will let you know about make a dianamic website and add some product in database and redirect them to the webpage then add button on product of "AddToCart" and then apply a click event and get product id and add it in your Session variable for cart . The first approact to make array and add data in array and then store array in the session arrays. Then your cart data is added in your session variable and now you can view your cart in your Show Cart page by looping through cart array.And you can remove item from cart also . I will explain every thing in steps below just fallow the all steps.


   Watch Video On Youtube


    Remove TO Cart








Create database and make php page where you want to show of your data in your webpage

Shopping cart system with php and mysql
Shopping cart system with php and mysql



Now here is the webpage where product are shown

Shopping cart system with php and mysql
Shopping cart system with php and mysql



Now you see button over the product is add to cart button when you click the button your product id will store into the cart array .


Shopping cart system with php and mysql
Shopping cart system with php and mysql



Now you see that cart is shown in the webpage and you can remove the cart also.


Here Is Code of Simple Cart System


Add To Cart Button 
<form method = "post" action = "home.php" >

<input type = "hidden" name = "id" value = "<?php echo $row['id'];?>" >
<input type = "submit" name = "adtocart" value = "Add To Cart" >

</form >


When Button is Press and get value in php Code

if (isset($_POST["adtocart"]))
{


$idd =$_POST['id'];
$itemArray = array($idd = > array('quantity' = > '1', 'code' = > $idd));

if (!empty($_SESSION["cart"])) {

$_SESSION["cart"] = array_merge($_SESSION["cart"], $itemArray);
} else
{

$_SESSION['cart'] = $itemArray;

}

}



Here is Show Cart Page Code


<?php
 
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phppractice";
$msg= "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
if(isset($_SESSION['cart'])){


$i=0;
 foreach ($_SESSION["cart"] as $item =>$k){
      
       //echo $k['code'];
  $val=$k['code'];

$sql = "SELECT * FROM product WHERE id='$val'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
$row = $result->fetch_assoc();

    ?>

    
 
<div class="product">
<a href="showproduct.php?id=<?php echo $row['id'];?>">
<img src="<?php echo $row["pimage"];?> " width="50px" height="50px" style="float:left;">
</a>

<p style="float: left;">Product Name : <?php echo $row["pname"];?></p><br>
<p style="float: left;">Product Price :<?php echo $row["pprice"];?></p>


<form method="post" action="cart.php">
  
  <input type="hidden" name="rcartid" value="<?php echo $item ;?>">
  <input type="submit" name="rmcart" value="Remove To Cart">
</form>

</div>




<?php


} else {
  echo "0 results";
}







}


}














?>


Here is Complete HomePage.php  File



<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phppractice";
$msg= "";
session_start();
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST["adtocart"]))
{


$idd=$_POST['id'];
$itemArray = array($idd=>array('quantity'=>'1', 'code'=>$idd));

if(!empty($_SESSION["cart"])) {

 $_SESSION["cart"] = array_merge($_SESSION["cart"],$itemArray);
  }else
  {
   
$_SESSION['cart'] = $itemArray;

  }

 
  

}


?>

<!DOCTYPE html>
<html>
<head>
  <title>Home</title>
  <style type="text/css">
    

.product{
display: inline-block;
border:1px solid black;
margin:10px;
width: 300px;
height: 450px;
}
.header{
  width: 100%;
  height: 60px;
  padding: 10px;
border-bottom: 1px solid black;
}
.footer{
   width: 100%;
  height: 400px;
  padding: 10px;
  background-color: black;
  color: white;
}

input[type="submit"]
{
  background-color: black;
  color:white;
}

  </style>
</head>
<body>
  <div class="header">
    <h1 style="color:orange;float: left;">E-commerance</h1>
    <a href="men.php" style="float: left;text-decoration: none;margin-top: 30px;margin-left: 80px">Men</a>
    <a href="women.php"  style="float: left;text-decoration: none;margin-top: 30px;margin-left: 80px">Women</a>
    <a href="kids.php"  style="float: left;text-decoration: none;margin-top: 30px;margin-left: 80px">Kids</a>
      <a href="cart.php" style="float: right;text-decoration: none;margin-top: 30px">Cart</a>
  </div>
  <h1 style="text-align: center;">Products</h1>

<?php


$sql = "SELECT * FROM product";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {

    ?>
    
 
<div class="product">

<img src="<?php echo $row["pimage"];?> " width="100%" height="300px">

<p>Product Name : <?php echo $row["pname"];?></p>
<p>Product Name :<?php echo $row["pprice"];?></p>

<form method="post" action="home.php">
  
  <input type="hidden" name="id" value="<?php echo $row['id'];?>">
  <input type="submit" name="adtocart" value="Add To Cart">

</form>

<a href="showproduct.php?id=<?php echo $row['id'];?>">Show Product</a>
</div>




<?php

  }
} else {
  echo "0 results";
}




?>

<div class="footer">
  <p style="text-align: center;">Copy right @ 3030</p>
</div>


</body>
</html>
  



Here Is ShowCartPage.php File

<?php

session_start();
if (isset($_POST["rmcart"])) {


 

 $value=$_POST["rcartid"];
if(!empty($_SESSION["cart"])) {
      foreach($_SESSION["cart"] as $k => $v) {
          if($value == $k)
            unset($_SESSION["cart"][$k]);        
          
      }
    }



}




?>



<!DOCTYPE html>
<html>
<head>
	<title>Cart</title>
	<style type="text/css">
		.product{
			display: inline-block;
			width: 100%;
			height: 100px;
			border:1px solid blue;

		}

.header{
  width: 100%;
  height: 60px;
  padding: 10px;
border-bottom: 1px solid black;
}
.footer{
   width: 100%;
  height: 400px;
  padding: 10px;
  background-color: black;
  color: white;
}

input[type="submit"]
{
  background-color: black;
  color:white;
}


	</style>
</head>
<body>
	  <div class="header">
    <h1 style="color:orange;float: left;">E-commerance</h1>
    <a href="#" style="float: left;text-decoration: none;margin-top: 30px;margin-left: 80px">Men</a>
    <a href="#"  style="float: left;text-decoration: none;margin-top: 30px;margin-left: 80px">Women</a>
    <a href="#"  style="float: left;text-decoration: none;margin-top: 30px;margin-left: 80px">Kids</a>
      <a href="cart.php" style="float: right;text-decoration: none;margin-top: 30px">Cart</a>
  </div>
<h1>Cart</h1>

<?php
 
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phppractice";
$msg= "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
if(isset($_SESSION['cart'])){


$i=0;
 foreach ($_SESSION["cart"] as $item =>$k){
      
       //echo $k['code'];
  $val=$k['code'];

$sql = "SELECT * FROM product WHERE id='$val'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
$row = $result->fetch_assoc();

    ?>

    
 
<div class="product">
<a href="showproduct.php?id=<?php echo $row['id'];?>">
<img src="<?php echo $row["pimage"];?> " width="50px" height="50px" style="float:left;">
</a>

<p style="float: left;">Product Name : <?php echo $row["pname"];?></p><br>
<p style="float: left;">Product Price :<?php echo $row["pprice"];?></p>


<form method="post" action="cart.php">
  
  <input type="hidden" name="rcartid" value="<?php echo $item ;?>">
  <input type="submit" name="rmcart" value="Remove To Cart">
</form>

</div>




<?php


} else {
  echo "0 results";
}







}


}














?>




<div class="footer">
  <p style="text-align: center;">Copy right @ 3030</p>
</div>


</body>
</html>






I hope You really enjoyed this blog . I try my best to make every thing is easy and helpfull to provide to public .You will get more blog about the Programing project and code . Thanks for visiting my blog if you have any querry related to this you can comment here i will try my best to response you back