PHP Classes

How to Implement a PHP Shopping Cart with MySQL using Session Variables to Store the Cart Items Using the Package Simple PHP Shopping Cart: Manage a shopping cart stored in database

Recommend this page to a friend!
  Info   Example   Screenshots   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-06-22 (3 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 88 All time: 9,980 This week: 38Up
Version License PHP version Categories
simple-php-shopping- 1.0.0The PHP License5PHP 5, Databases, E-Commerce, Global
Description 

Author

This package can manage a shopping cart stored in a database.

It provides a PHP script that implements operations to manage the items of a shopping cart using PHP sessions to store the shopping cart items and a MySQL database to store the list of available products.

Currently, it can:

- Add an item with a given quantity of products to the shopping cart

- Remove items from the shopping cart

- Empty the shopping cart

- List the products in the shopping cart

- List the products available for sale

Picture of Adeleye Ayodeji
  Performance   Level  
Name: Adeleye Ayodeji <contact>
Classes: 25 packages by
Country: Nigeria Nigeria
Age: ???
All time rank: 274719 in Nigeria Nigeria
Week rank: 21 Up2 in Nigeria Nigeria Up
Innovation award
Innovation award
Nominee: 15x

Example

<?php
session_start
();
require_once(
"dbcontroller.php");
$db_handle = new DBController();
if(!empty(
$_GET["action"])) {
switch(
$_GET["action"]) {
    case
"add":
        if(!empty(
$_POST["quantity"])) {
           
$productByCode = $db_handle->runQuery("SELECT * FROM tblproduct WHERE code='" . $_GET["code"] . "'");
           
$itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["name"], 'code'=>$productByCode[0]["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"], 'image'=>$productByCode[0]["image"]));
           
            if(!empty(
$_SESSION["cart_item"])) {
                if(
in_array($productByCode[0]["code"],array_keys($_SESSION["cart_item"]))) {
                    foreach(
$_SESSION["cart_item"] as $k => $v) {
                            if(
$productByCode[0]["code"] == $k) {
                                if(empty(
$_SESSION["cart_item"][$k]["quantity"])) {
                                   
$_SESSION["cart_item"][$k]["quantity"] = 0;
                                }
                               
$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
                            }
                    }
                } else {
                   
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
                }
            } else {
               
$_SESSION["cart_item"] = $itemArray;
            }
        }
    break;
    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;
}
}
?>
<HTML>
<HEAD>
<TITLE>Simple PHP Shopping Cart</TITLE>
<link href="style.css" type="text/css" rel="stylesheet" />
</HEAD>
<BODY>
<div id="shopping-cart">
<div class="txt-heading">Shopping Cart</div>

<a id="btnEmpty" href="index.php?action=empty">Empty Cart</a>
<?php
if(isset($_SESSION["cart_item"])){
   
$total_quantity = 0;
   
$total_price = 0;
?>
<table class="tbl-cart" cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th style="text-align:left;">Name</th>
<th style="text-align:left;">Code</th>
<th style="text-align:right;" width="5%">Quantity</th>
<th style="text-align:right;" width="10%">Unit Price</th>
<th style="text-align:right;" width="10%">Price</th>
<th style="text-align:center;" width="5%">Remove</th>
</tr>
<?php
   
foreach ($_SESSION["cart_item"] as $item){
       
$item_price = $item["quantity"]*$item["price"];
       
?>
<tr>
                <td><img src="<?php echo $item["image"]; ?>" class="cart-item-image" /><?php echo $item["name"]; ?></td>
                <td><?php echo $item["code"]; ?></td>
                <td style="text-align:right;"><?php echo $item["quantity"]; ?></td>
                <td style="text-align:right;"><?php echo "$ ".$item["price"]; ?></td>
                <td style="text-align:right;"><?php echo "$ ". number_format($item_price,2); ?></td>
                <td style="text-align:center;"><a href="index.php?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction"><img src="icon-delete.png" alt="Remove Item" /></a></td>
                </tr>
                <?php
                $total_quantity
+= $item["quantity"];
               
$total_price += ($item["price"]*$item["quantity"]);
        }
       
?>

<tr>
<td colspan="2" align="right">Total:</td>
<td align="right"><?php echo $total_quantity; ?></td>
<td align="right" colspan="2"><strong><?php echo "$ ".number_format($total_price, 2); ?></strong></td>
<td></td>
</tr>
</tbody>
</table>
  <?php
} else {
?>
<div class="no-records">Your Cart is Empty</div>
<?php
}
?>
</div>

<div id="product-grid">
    <div class="txt-heading">Products</div>
    <?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 class="product-tile-footer">
            <div class="product-title"><?php echo $product_array[$key]["name"]; ?></div>
            <div class="product-price"><?php echo "$".$product_array[$key]["price"]; ?></div>
            <div class="cart-action"><input type="text" class="product-quantity" name="quantity" value="1" size="2" /><input type="submit" value="Add to Cart" class="btnAddAction" /></div>
            </div>
            </form>
        </div>
    <?php
       
}
    }
   
?>
</div>
</BODY>
</HTML>


Details

simple-php-shopping-cart

Simple PHP Shopping Cart


Screenshots (4)  
  • product-images/camera.jpg
  • product-images/external-hard-drive.jpg
  • product-images/laptop.jpg
  • product-images/watch.jpg
  Files folder image Files (11)  
File Role Description
Plain text file dbcontroller.php Class Class source
Accessible without login Image file icon-delete.png Icon Icon image
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file style.css Data Auxiliary data
Accessible without login Plain text file tblproduct.sql Data Auxiliary data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:88
This week:0
All time:9,980
This week:38Up