Creating Session

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Creating Session

Post 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....
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post 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";
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post 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.
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post 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.
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post by Zeceer »

Like an array? But what about customers who are buying let's say 5 products?
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post 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.
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post 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)
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post 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).
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post 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?
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post 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
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post 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
Post Reply