I am writing a script in php, which is quite similar to a shopping cart. what i want to do is when a users adds a certain product i need to add the productid to a session variable,without storing in a database. so each time the user adds a product the productid needs to be stored in a session variable.
and when the user checkouts i need to retrieve all the productids and display?
can some one please explain me how to do it? coz im fine with 1 product but not sure how to store and retrieve multiple values.
any help will be much appreciated
Place an Array in the Session. Add the items to the array.
Note: replace
$apples,$orangesand$pearswith your product ids.You access the array like any other array in PHP, e.g. to count the items:
and to iterate over the items:
You could also wrap the Session into an object and provide access to the cart through a method interface, but I leave that for someone else to explain.