Page 1 of 1

Creating Session

Posted: Sat Jun 21, 2003 7:56 am
by Zeceer
Hi,

I'm making a webshop and are gonna use session to register products that the customer put in his cart.

The products are going to be registered like this. When clicking the button to buy a products, it sends the product number with it in the link.

The add script i then gonna make a session that has this product number. The first session registered are going to be called 1, and will have the product number in it. When buying a second product the script checks if 1 allready is registered, and if it is, it increases the variable to 2, and so on.

The problem is have to make this work? Increasing the name on tha variable created?

This is what I've done so far since I can't get any further :oops:

Code: Select all

<?php
session_start();

$number="1";

while( session_is_registered("$number") )
&#123;
        ++$number;
&#125;



session_register("number");

?>
As you see when I register number, the variable are going to be named number, and not 1 as I want it to. I want the variable that are beeing registered to be named 1, and have the products number in it. This seems kind of hard to do....

Posted: Sat Jun 21, 2003 8:18 am
by delorian
Why the hell :D do you want to have the variable name was the same as its value :?: The number value isn't good enough :?:

BTW: $number=1 is better (but equal) than $number="1";

Posted: Sat Jun 21, 2003 8:37 am
by Zeceer
Not the same. The variable are going to be 1, and the value is the product number sent with the link of the buy button customers use.

Posted: Sat Jun 21, 2003 8:42 am
by delorian
Co can you do it by two dimensional table like:

Code: Select all

$cart['product_id']['quantity'];
You can send the product id by the url and increase the quantity if the variable is registered.

Posted: Sat Jun 21, 2003 8:55 am
by Zeceer
Like an array? But what about customers who are buying let's say 5 products?

Posted: Sat Jun 21, 2003 9:03 am
by delorian
So you will have something like this:

Code: Select all

$cart['product_id']['quantity'];
/* and it will have the values:
----------------------------------------------
product_id         | quantity
----------------------------------------------
34534               | 4
45645               | 3
43535               | 44
23324               | 1
etc. etc.
----------------------------------------------
*/
And yes of course it's an array.
You will have to check product_id to know which quantity you should increase.

Posted: Sat Jun 21, 2003 9:07 am
by Zeceer
That's it! But have do I register that in a session? Can't do

session_register("$cart[gfgf][fgg]"); (just an example)

Posted: Sat Jun 21, 2003 9:13 am
by delorian
Zeceer wrote:That's it! But have do I register that in a session? Can't do

session_register("$cart[gfgf][fgg]"); (just an example)
Yes you should register that in session. But try using $_SESSION variables array instead of session_register (if you have PHP 4.1.0 and up).

Posted: Sat Jun 21, 2003 9:26 am
by Zeceer
What about adding more products at a later point? I've never really done this before, so do you have any possibility of showing me how, by using som code examples?

Posted: Sat Jun 21, 2003 3:28 pm
by delorian
Zeceer wrote:What about adding more products at a later point?
There's nothing more to add. You must understand that the $cart array is just a session variable. It doesn't have value for all products. The id and quantity is inserted only when client add the product into his/her cart.
Zeceer wrote:I've never really done this before, so do you have any possibility of showing me how, by using som code examples?
Me either :D Just giving you some ideas. Sorry I don't have any code examples.


BTW: http://phpbuilder.com - has some really great articles about shopping carts

Posted: Sat Jun 21, 2003 4:02 pm
by cactus

Posted: Sat Jun 21, 2003 4:31 pm
by Zeceer
Thank you! I will read thru the articles + checking out the array section in my PHP books. So I will probably figure something out :D