How to make a shopping cart

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

How to make a shopping cart

Post by Tokunbo »

hello sirs,

Kindly, I need some help and explanation: I'm trying to study how to build a shopping cart.

Presently, I have a form with 2-fields.: a product and a quantity field. The product field is a list with options (item1, item2, item3, item4). So a user basically selects and options and enters a value for the "quantity" input field; and then clicks the submit. I have this working ok.

What I want help with is how to display what the user selected in a table (usually called a cart) and how to have it add the users selections to the table - after clicking the submit button.

Ive done some reading about either using / not using a database to store inputs from the user. With the above example, I do not think I need a database. So pls kindly help with how to temporarily store the users input and then display them in a table.

warm regards
Tokunbo
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: How to make a shopping cart

Post by nowaydown1 »

Tokunbo,

Welcome to the forum. If your intention is to simply persist the data temporarily and display it, use sessions. Sessions are used to keep state between page requests. So your user selects a product from the list, and a quantity. You could store that information in a multi-dimensional array like so:

Code: Select all

$_SESSION['cart'][$productID] = $productQuantity;
Then you could simply loop over your cart contents:

Code: Select all

foreach($_SESSION['cart'] as $productID => $productQuantity) {
      /* Build your display table here */
}
That's fairly primitive, but that's the basic idea. Then, you can make on a judgement call on when you flush the information to your storage mechanism. Some systems store "abandoned" cart information, some don't.

Hope that helps.
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

Re: How to make a shopping cart

Post by Tokunbo »

hi there,

thanks for the reply.

Yes, all I need is to simply display the data - whatever the user chooses. For example, if the user selects option-1 and enters "5" inside the quantity field, I want my table's row-1 to be updated with option-1 and the quantity-field value.

The user could still pull down the menu and select another product and add a corresponding quantity - I would also want my table to be updated with the users choices - row2 of table

It is the content of the table(list of users choices) that I want to send to an email.

Another question: suppose I have multiple drop down menus on a page; lists are divided into categories, for example:

1) product-category-1 is a pull down list with several options(products) from which the user can make a choice / choices as I described above. The user has to click the submit button for every choice made - in order for the table to be updated. So lets assume the user makes two choices in this category before moving on to another category. The table now has two rows
2) product-category2 is also a pull down list with several options(products) from which a user can make selections, etc This category would also have its own submit button. Lets assume the user has only 1-selection here, so after clicking the submit button, the table is again updated - now table has three rows.

So I would want to email the users choices - 3-rows of chosen products and individual quantities.

Questions:
1) can I still use sessions in this case?
2) with several submit buttons, will they be able to submit to the same table; Or?

pls advise.

regards
Tokunbo
Post Reply