Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding to Cart - Sessions and Arrays

Status
Not open for further replies.

NickyJay

Programmer
Sep 1, 2003
217
GB
Hi!

Wondering if anyone can help/poing me in the direction of a suitable tutorial...

I'm just starting out with php and have a simple set of pages i need to add php to. I have a product page of three items,each item has its own form and submit button - the value of the submit is the name of the item - not the way i would have done it but these are the pages i have been given!

Basically, each button is an Add button that should add the item to a session and redirect the user to a cart page that summarises the items selected - i am able to add one item, but when i choose to go to another product page and add another item, this item writes over the first one so i only have the one item (last one added) in my cart!

I want to be able to add more than one item, more than once (page only allows one of the item to be added at a time - no way to edit the quantity - again, not my design or way of doing it...)

I understand i need to add the items to an array in my session - how would i do this? I am new to the array concept, go easy on me!

Also, on my cart page i want to be able to count how many of each item has been added - instead of listing each item seperatly, where one has been added 3 times, i want a count to show item = 3.

Please can someone help?!

Thanks so much for taking the time to read my ramblings!!
 
Think of an array as file cabinet. each drawer is for one item type. So if you add another item to the array, its like putting the new item into its proper drawer. so instead of having just the single item in the drawer, yu have 2 or 3 etc...
Suppose you click the button to add n item to the array:
Code:
[greenn]\\Get the array from the sesion:[/green]

$items=$_SESSIONS['items'];

then add the new item to ts drawer:

$items['itemX']=$item['itemX']+1;
[green]This adds one more entry to the drawer for that item[/green]

you can then write the array to session variable:

$_SESSION['items']=$items;

Then when you want to see how many of the items have been added you can do something like

echo "There are $items['itemX'] units in the cart of itemX";
echo "There are $items['itemY'] units in the cart of itemY";  

etc...

You can keep adding item slots to the array if you add more items n the future.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hiya,

thanks so much for responding, i get what your saying but now need to fit it to my example (here's my problem!)

As i mentioned before, my items each have their own form, basically with a hidden field -
Code:
<input type="hidden" name="cartaction" value="add" />
and a submit button -
Code:
<input type="hidden" name="item" value="itemX" />

Should i be using the
Code:
$items['itemX']=$item['itemX']+1;
in each form??

Sorry if this is abit basic and commone sense, i've been programming in Coldfusion for about a year and half and haven't done anything similar for me to look at! As i say, im new to php, but think i need to learn fast!

[smile]
 
You could do it only once, if you apply the value from the submit buttons.

Something like:

Code:
if(isset($_POST['item'])){ 
$items['$_POST['item']']=$items['$_POST['item']']+1;
}


That will automatically check if a submit button has been pressed, and using the value for that submit button $_POST{'item'] will increase the item counter inside by one.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top