Shopping cart - Submitting multiple items in one form

Associate
Joined
21 Sep 2007
Posts
28
Hi

A client has asked that i implement the equivalent of a bulk order form which consists of multiple items, each with an option to enter a description and quantity so the table.

i have attached a screen shot to illustrate how data s captured. For each row of items i have a hidden input which contains the value of the product id.

r8s5zl.jpg



When a customer enters details for each product i need to store this in a session variable so that i may later add the order details to the database on the order confirmation page.

My only problem is that im not to sure how to store the form variables in a session in which i can later loop over and then add to the DB. can any body please advise. I'll provide any information on request. Thank you.
 
Last edited:

Pho

Pho

Soldato
Joined
18 Oct 2002
Posts
9,324
Location
Derbyshire
You could create a class, i.e. :

(random psuedo code)
PHP:
class order_product {
	$id
	$quantity
	$description
}

So for each 'line' you create an instance of the class and save it into an array for their shopping cart, and then into the session. Or you could use a database.
 
Associate
OP
Joined
21 Sep 2007
Posts
28
is that akin to storing objects in a session variable? I have very basic php knowledge and im using this as a platform to improve this.However if you dont mind could you please expand on your code?

Also you suggestion to use a database to store the session. does that mean that i would then have to write a scheduled script that removes abandoned carts? e.g after 60 mins?
 
Associate
Joined
1 Mar 2004
Posts
1,930
Location
Farnborough, Hants
As Pho suggested, each line in your diagram would represent one object (say OrderProduct), and the whole lot would be in one collection (OrderProductList for example). This list would go into session state, and you'd recall it when you are ready to squirt it into your db.

Off the top of my head (in C#):

Code:
//Product Class
public class Product
{
      public string Name {get; set; }
      public int Quantity {get; set; }
}

...
//create a product collection
List<Product> myListForSessionState = new List<Product>();

//create a product object
Product prod = new Product();

/assign some values
prod.Name = "Leeks";
prod.Quantity = 5;

//add it to the collection
myListForSessionState.Add(prod);

//finally, add the collection to session state
Session.Add("ProductList", myListForSessionState);
 
Last edited:
Back
Top Bottom