Session handling in a PHP based shopping cart

We can build a simple and fully functional shopping cart for our own use with the help of PHP. For this, all you need to have is the knowledge of PHP sessions and array functions. So rather than discussing what PHP sessions are and how they work, we would move straight to the actual work to do, that is, building a shopping cart. This shopping cart would basically have four tables, although the actual shopping process would require only one table. The other three tables would be used to place successful orders. So the “product” table would be used to add products to the shopping cart. “Customers” table would be used to place orders and have billing details. In the “orders” table, there would be all the entries of the successful orders. And finally, “order detail” would list the details of the items purchased.

The process would be 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 and finally, the PHP script redirects to a different page (shoppingcart.php) which displays purchased items to the user. And the code to store these 3 items in the session array is as follows:

235, 239 and 287 are the Product IDs.

Three main utility functions would be involved in the entire code. Those are:

This above function makes sure that the session variable is initialized and then stores the received ‘productid’ and ‘quantity’ to next available index. And note that there is no need to increment the $max variable

The above function goes through all the elements of shopping cart and checks if products exist in the shopping cart.

And the above 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.

The above code would be used to create a simple but fully functional shopping cart which you can use for you own use.

Looking for a quality hosting service? here are a few I suggest.
BlueHost | HostGator | Media Temple
Ali has been an entrepreneur in web, video and related technologies. Having worked with many business across the globe, Ali stands truly a great pillar in the business working with him.