PHP Shopping Cart

PHP Shopping Cart


PHP Shopping Cart
PHP Shopping Cart


php product shopping cart application. In this tutorial we are going to see a PHP example for creating shopping cart application. This shopping cart application is purposely kept as simple as possible. php shopping create own site.  In this example, we are displaying list of products from database. For each product, we have given options for selecting product quantity and adding to cart. The cart items are stored in a session. We can clear this session by removing items from the cart. cart Page Image

Adding Products  to Shopping Cart

Normal PHP Shopping Cart Product added Simple Code for Follows

<?php
$product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");
if (!empty($product_array)) { 
foreach($product_array as $key=>$value){
?>
<div class="product-item">
 <form method="post" action="index.php?action=add&code=<?php echo $product_array[$key]["code"]; ?>">
 <div class="product-image"><img src="<?php echo $product_array[$key]["image"]; ?>"></div>
 <div><strong><?php echo $product_array[$key]["name"]; ?></strong></div>
 <div class="product-price"><?php echo "$".$product_array[$key]["price"]; ?></div>
 <div><input type="text" name="quantity" value="1" size="2" /><input type="submit" value="Add to cart" class="btnAddAction" /></div>
 </form>
</div>
<?php }} ?>

Removing Cart Item

Product Adding to the shopping cart, and following switch case will be executed this code to store selected items information and details into the session. If the selected item is already in session inthe cart, and then the quantity will be checked whether it is changed. If so, it will be updated.

case "remove":
 if(!empty($_SESSION["cart_item"])) {
  foreach($_SESSION["cart_item"] as $k => $v) {
   if($_GET["code"] == $k) unset($_SESSION["cart_item"][$k]);    
   if(empty($_SESSION["cart_item"])) unset($_SESSION["cart_item"]);
  }
 }
break;
case "empty":
 unset($_SESSION["cart_item"]);
break;

Shppoing Cart Store Database MySql Query


Shopping cart selected Item Product stored database
CREATE TABLE IF NOT EXISTS `tblproduct` (
  `id` int(8) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `code` varchar(255) NOT NULL,
  `image` text NOT NULL,
  `price` double(10,2) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `product_code` (`code`)
)
PHP Shopping Cart Dev2Tricks 5 of 5
PHP Shopping Cart PHP Shopping Cart php product shopping cart application. In this tutorial we are going to see a PHP example f...

Share this

Related Posts

Previous
Next Post »