Create a shopping cart Part I

Create a shopping cart Part I

Hello, friends!
As is promised some time ago, i started writing tutorials about basic programming concepts exclusively for insanelyart.com;

This is the first tutorial of my upcoming collection and i hope that admins will approve it, but not least, i hope that it will be useful to someone and that you will like it.

In this tutorial i will show you how a basic shopping cart works, and how to build your first PHP shopping cart.

First of all, let's imagine a supermarket crowded by people buying goods. Every client has it's own shopping cart and that's why we need to keep track of everyone's purchases without mixing them up with each other. The simplest way to do this is giving every shopping cart a unique ID. The products that the clients will buy will also have a unique ID. Finally, we will have to link the single shopping cart with the client. In this following example, users do not need to register to shop so we will not have a list of users to link user name and shopping cart ID. We can use several other methods instead, but i will explain this further during the tutorial. The next image is a schema of what written up here:



In the upper illustration you can see three tables, everyone used to store different information.
We have a table that contains data about every item, with a random generated ID, like:

ID: 47JER32DN0
NAME: Water Melons
PRICE: 1,95
DESCRIPTION: Fresh water melons. Country of provenience: Turkey.

The ID is also the primary key of the table witch means that it can not be duplicated (it will be impossible to have two items with the same ID).

The table that contains data for every shopping cart is made pretty much like the items table, every shopping cart having it's own unique ID, so there can not be two shopping carts with the same ID, just like the items.

After building the two tables, we have to link it with each other and for that reason we will need a third table called rel because we need a many to many kind of relationship. A many to many kind of relationship stands for 1 item can be found in more than 1 shopping cart, 1 shopping cart can contain more than 1 item.

The table rel has a primary key composed of two elements that can not be duplicated together. To make more sense out of it, take a look at the image bellow:



The section in red shows what explained earlier.

Ok, it's time to create the three tables using the code bellow:




Pay attention at the data types used for the creation of the tables.
The id's are VARCHAR(10) which means that the ID will be alphanumerical composed of 10 characters and that it can not be null. Another important thing is that the connected fields between the tables must be the same data type.
The price of every item is FLOAT data type so we can store prices with decimals.

Download the code and insert it into your MySql. You should obtain:



Congratulations! You have now created the basis of your shopping cart. What's left to do is linking every single shopping cart with the client.

Considering that the users do not need to register in to place an order, we have to think about how to resolve this problem. A way of doing it is to link the tutorial's ID with the user's IP address, but what about users that have a dynamic IP address that changes every time they connect? This means that the link between the user and it's cart will be lost.

Another way of linking users and carts without forcing the user to register is through cookies.
You can set a cookie containing the shopping cart's ID related to the user and ask for the cookie every time the user accesses you website.
Watch the following example of code:




In case the cookie is not set, you can choose to create the user's personal shopping cart when he adds an item to it for the first time, or as soon as he visited your website for the first time.

I will create the shopping cart as soon as he accessed the page using the code bellow:




The function that generates the ID code could be built like this:




I have chosen to set the lifetime of the cookie for one month, so the user can still view the items he added to the cart even after a month (you can set it for an year if you want to).

If the user already has a shopping cart linked to him, we can perform several tasks in order to build the content of the page.

For example, we can count the number of items he has added into it, we can calculate the total expense he is heading to, we can see exactly what items he chosen to buy.

Remember that the table items is connected to the table carts so i will give you an example of how to view the content of the user's shopping cart. Take a look at the code bellow:




Of course, this is not the only thing you can do, but i will show you more interesting stuff in the next part of the tutorial, so this is about all for today.

Stay tuned and come by tomorrow and i will show you how to add items to the shopping cart, how to delete items from the shopping and how to place an order with the content of the shopping cart.

If you want to see this shopping cart at work, you can visit bijushop.asoc.ro .

I will consider providing this script for free as after i will have written all the parts of the tutorial.

For now, i will only provide a piece of code related to this part of the tutorial.

I hope you enjoy it and do not be shy to post a comment if you have further questions.

See you soon!

Download Resource

Download

68 Responses to this Post!

  1. rafi says:

    very nice and post more tutorials

  1. m16u31_ says:

    nice,,,, this a great tutorial

  1. mafiaxp says:

    thank you

  1. sudhan says:

    good one

  1. valoni says:

    VERY VERY NICE TUTORIAL

  1. tranzitor says:

    good

  1. abdurob says:

    thanksrn

  1. akila says:

    yea its nice...go ahead

  1. mitle says:

    Great Tutorial. Thankyou

  1. yana says:

    nice tutorial... i try to build a shopping cart with this info.. thanks

Leave a comment and earn 1 credit

Log In to post a comment.
Sign Up if you are not yet a member