Simple Ecommerce Shopping Cart PHP mysql

Mysql Mysqli php Programming

Simple Ecommerce Shopping Cart PHP mysql

In this Post We Will Explain About is Simple Ecommerce Shopping Cart PHP mysql With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Create Simple Shopping Cart with PHP and MysqlExample

In this post we will show you Best way to implement PHP Shopping Cart Tutorial – Step By Step Guide!, hear for Simple PHP Shopping Cart using Sessionswith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Table structure for table `live_shoping_cart

 CREATE TABLE IF NOT EXISTS `live_shoping_cart` (  
  `id` int(11) NOT NULL AUTO_INCREMENT,  
  `name` varchar(255) NOT NULL,  
  `product_image` varchar(255) NOT NULL,  
  `product_price` double(10,2) NOT NULL,  
  PRIMARY KEY (`id`)  
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;  
 --  
 -- Dumping data for table `live_shoping_cart`  
 --  
 INSERT INTO `live_shoping_cart` (`id`, `name`, `product_image`, `product_price`) VALUES  
 (1, 'Laptop', 'Laptop.jpg', 98985.00),  
 (2, 'Iphone', 'Iphone.jpg', 58596.00),  
 (3, 'Redmi Mobile', 'Redmi-Mobile.jpg', 10025.00);

index.php

<?php   
 session_start();  
 $connect = mysqli_connect("localhost", "root", "[email protected]#$live", "live24u");  
 if(isset($_POST["product_add_cart"]))  
 {  
      if(isset($_SESSION["custom_shoping_cart"]))  
      {  
           $product_array_id = array_column($_SESSION["custom_shoping_cart"], "product_id");  
           if(!in_array($_GET["id"], $product_array_id))  
           {  
                $count = count($_SESSION["custom_shoping_cart"]);  
                $product_array = array(  
                     'product_id'               =>     $_GET["id"],  
                     'product_name'               =>     $_POST["cart_hidden"],  
                     'product_pr'          =>     $_POST["pr_hidden_val"],  
                     'product_quantity'          =>     $_POST["quantity"]  
                );  
                $_SESSION["custom_shoping_cart"][$count] = $product_array;  
           }  
           else  
           {  
                echo '<script>alert("Product Already Added")</script>';  
                echo '<script>window.location="index.php"</script>';  
           }  
      }  
      else  
      {  
           $product_array = array(  
                'product_id'               =>     $_GET["id"],  
                'product_name'               =>     $_POST["cart_hidden"],  
                'product_pr'          =>     $_POST["pr_hidden_val"],  
                'product_quantity'          =>     $_POST["quantity"]  
           );  
           $_SESSION["custom_shoping_cart"][0] = $product_array;  
      }  
 }  
 if(isset($_GET["action"]))  
 {  
      if($_GET["action"] == "product_remove")  
      {  
           foreach($_SESSION["custom_shoping_cart"] as $keys => $values)  
           {  
                if($values["product_id"] == $_GET["id"])  
                {  
                     unset($_SESSION["custom_shoping_cart"][$keys]);  
                     echo '<script>alert("Product Removed")</script>';  
                     echo '<script>window.location="index.php"</script>';  
                }  
           }  
      }  
 }  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>live24u Tutorial | Live24u PHP Mysql Shopping Cart</title>  
           <script src="jquery.min.js"></script>  
           <link rel="stylesheet" href="bootstrap.min.css" />  
           <script src="bootstrap.min.js"></script>  
      </head>  
      <body>  
           <hr />  
           <div class="container" style="width:700px;">  
                <h3 align="center">Live24u PHP Mysql Shopping Cart</h3><hr />  
                <?php  
                $sql_query = "SELECT * FROM live_shoping_cart ORDER BY id ASC";  
                $result = mysqli_query($connect, $sql_query);  
                if(mysqli_num_rows($result) > 0)  
                {  
                     while($row = mysqli_fetch_array($result))  
                     {  
                ?>  
                <div class="col-md-4">  
                     <form method="post" action="index.php?action=add&id=<?php echo $row["id"]; ?>">  
                          <div style="border:2px solid #666; background-color:#D3D3D3; border-radius:4px; padding:12px;" align="center">  
                               <img src="<?php echo $row["product_image"]; ?>" class="img-responsive" /><hr />  
                               <h4 class="text-info"><?php echo $row["name"]; ?></h4>  
                               <h4 class="text-danger">$ <?php echo $row["product_price"]; ?></h4>  
                               <input type="text" name="quantity" class="form-control" value="1" />  
                               <input type="hidden" name="cart_hidden" value="<?php echo $row["name"]; ?>" />  
                               <input type="hidden" name="pr_hidden_val" value="<?php echo $row["product_price"]; ?>" />  
                               <input type="submit" name="product_add_cart" style="margin-top:5px;" class="btn btn-success" value="Add to Cart" />  
                          </div>  
                     </form>  
                </div>  
                <?php  
                     }  
                }  
                ?>  
                <div style="clear:both"></div>  
                <hr />  
                <h3>Order Details</h3>  
                <div class="table-responsive">  
                     <table class="table table-bordered">  
                          <tr>  
                               <th>Product Name</th>  
                               <th>Quantity</th>  
                               <th>Price</th>  
                               <th>Total</th>  
                               <th>Action</th>  
                          </tr>  
                          <?php   
                          if(!empty($_SESSION["custom_shoping_cart"]))  
                          {  
                               $product_total = 0;  
                               foreach($_SESSION["custom_shoping_cart"] as $keys => $values)  
                               {  
                          ?>  
                          <tr>  
                               <td><?php echo $values["product_name"]; ?></td>  
                               <td><?php echo $values["product_quantity"]; ?></td>  
                               <td>$ <?php echo $values["product_pr"]; ?></td>  
                               <td>$ <?php echo number_format($values["product_quantity"] * $values["product_pr"], 2); ?></td>  
                               <td><a href="index.php?action=product_remove&id=<?php echo $values["product_id"]; ?>"><span class="text-danger">Remove</span></a></td>  
                          </tr>  
                          <?php  
                                    $product_total = $product_total + ($values["product_quantity"] * $values["product_pr"]);  
                               }  
                          ?>  
                          <tr>  
                               <td colspan="3" align="right">Total</td>  
                               <td align="right">$ <?php echo number_format($product_total, 2); ?></td>  
                               <td></td>  
                          </tr>  
                          <?php  
                          }  
                          ?>  
                     </table>  
                </div>  
           </div>  
           <hr />  
      </body>  
 </html>

Example

I hope you have Got What is simple shopping cart in php source code And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.


Related FAQ

Here are some more FAQ related to this Article:

  1.   react open link in new tab programmatically
  2.   Two Way Data Binding using Angular Example
  3.   ng-minlength, ng-maxlength Validation using Angular Example
  4.   Ajax Shopping Cart PHP MYSQLi Tutorial With Example
  5.   PHP program to print patterns of numbers and stars
  6.   Insert Update Delete using PHP and mysql for Frontaccounting - crud
  7.   Custom Pagination Using WooCommerce REST API
  8.   PHP E-Commerce Shopping Cart Application step by step
  9.   Form Input Fields Validation using Angular Example
  10.   Simple Ecommerce PHP Shopping Cart Application

Leave a Reply

Your email address will not be published. Required fields are marked *