Shopping cart in PHP Class

    Rating: ★★★★☆
    View: 232
    Download: 178
    Từ khóa:

    First of all i would like to discuss the database structure for this shopping cart. The shopping cart will have 4 tables, although the shopping process requires only 1 table i.e products, the other three tables (customers, orders and order_detail ) will be required to store successful orders. The structure of all these tables is shown below

    shopping cart relationship diagram

    Database Tables

    Products The products in this table will be shown to the user, she will add these products to her shopping cart
    Customers When the user places order, her billing detail will be stored in this table
    Orders There will be a single entry for each successful order. Each order will be assigned a unique serial/order number
    Order Detail There will be a single entry for each item purchased. Although price is available from the products table, but if the price of an item changes in the products table, it should not be changed in the order_detail table. So these are two different fields.


    Lets now come to PHP. The shopping process will work like this:

    • The user adds a product to his/her shopping cart
    • The PHP script (products.php) receives this action and stores the requested product in the session array
    • The PHP script redirects to a different page (shoppingcart.php) which displays purchased items to the user.

    How the products will be stored in the session array ?

    The cart will be a 2 dimensional session array to store productid and quantity of every item purchased. The table below shows 3 items in the shopping cart.

    Index productid qty
    0 235 1
    1 239 2
    2 287 1

    And the code to store these 3 items in the session array, is given below


    Important Shopping Cart Functions

    The following utility functions are involved in the process of shopping

    function addtocart($pid,$q){
    	if($pid<1 or $q<1) return;
    		if(product_exists($pid)) return;

    The addtocart function makes sure that the session variable is initialzed and then stores the received productid and quantiy to next available index. And note that there is no need to increment the $max variable

    function product_exists($pid){
    	return $flag;

    The function goes through all the elements of shopping cart and checks if a products exists in the shopping cart

    function remove_product($pid){

    The remove_product function first finds the product and then removes the corresponding index from the session array. The last statement  

     {$_SESSION['cart']=array_values($_SESSION['cart']) }

     resets the array indexes.

    About PHP files in this tutorial

    There are only 3 php files in the demo (in addition to functions.php and db.php file)

    • products.php (displays products to the user)
    • shoppingcart.php (displays items in the shopping cart to the user)
    • billing.php (updates customer billing information in the database)
    loading Đang tải...

    template được ưa chuộng