...

View Full Version : Shopping cart info



anarchy3200
05-12-2004, 05:51 PM
Hi,

i am making a shopping cart but got stuck on what i should use to store the values,

e.g. name and price of multiple items

should i store this all into a string or something else


any help appreciated

bcarl314
05-12-2004, 07:29 PM
I think your best bet would be to use sessions.

anarchy3200
05-12-2004, 07:50 PM
sessions would be easiest but how would i set the multiple items

at the moment i have:


$_SESSION['name'] = 'Guitar<del>15.99'

to set the value (these are set from a get)

and i have


@list($name, $price) = explode("<del>", $_SESSION['name']);

echo $name;
echo $price;

to view it but this will only handle 1 item, how would i expand this?

Thanks for any help.

bcarl314
05-12-2004, 08:03 PM
Hopefully this will help...

Use arrays to hole the quantity, itemID (sku) and what ever else. See example:



<?php
session_start();
$_SESSION['sku'][0]="01-003-sku";
$_SESSION['qty'][0]=1;
$_SESSION['sku'][1]="05-33-14da3";
$_SESSION['qty'][1]=2;
?>
<a href="page2.php">See session info</a>


Now on the second page (page2.php) I can print them out (or modify them if I want...



<?php
session_start();
print "<pre>";
print_r($_SESSION);
print "</pre>";
?>


Hope this helps.

bcarl314
05-12-2004, 08:07 PM
Or, on page 1 you can even do this...



<?php
session_start();
$_SESSION['items']["01-003-sku"]=1;
$_SESSION['items']["05-33-14da3"]=3;
?>
<a href="page2.php">See session info</a>

anarchy3200
05-12-2004, 08:23 PM
last questions for now,

what does the <pre> bit do?

and how would i view the information on its own without the layout around it?


thanks for all the help. :thumbsup:

raf
05-12-2004, 08:42 PM
Why don't you immedeately write it to the db or to a persistent cookie?
Otherwise all info is lost when the session is terminated.

You'll need to write it to the db sooner or later so why first drag it along in a session-variable?

anarchy3200
05-12-2004, 08:46 PM
i'm not using a db but how would i do a similar thing and write it to a cookie

thanks for any help.

would i just be able to swap the session for cookie and is there an easy way to just roll off all the arrays of the cookie in a list

raf
05-12-2004, 09:12 PM
wait a minute.

If you don't use a db and would store it inside a cookie or session, how do you then get the bought items? I hope you're not planning on have them mailed to you.
You do realise that yu don't have control over the session and cookiedata? The sessiondata is just removed when the session end, and the cookie is stored on the client.

Storing i in a cookie is as simple as

setcookie ('selected', $array_shoppigcart, time()+31536000); // expires after 1 year

to get the data:

$_COOKIE['selected']

$array_shoppigcart is then the array with your products

anarchy3200
05-12-2004, 09:34 PM
what i am going to do is store it through the session and then it will be saved to a db properly when the customer goes to the checkout so that i am not saving data that does not come out to an order.

sorted it now so thanks everyone for the help

Mike



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum