Session for products

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
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Session for products

Post by ianhull »

Hi Guys,

I am trying to think of a way to store multiple products in a session like a little shopping cart but I am confused....it does not take much :)

I know how to register 1 product in a session and retrieve it but how would I add more without overwriting the first one?

Thanks in advance for any advice.
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

I am BRAND new at sessions, but I think you can just add more vars to the session, like this :

Code: Select all

session_start();

                        /* USERNAME */
			$_SESSION['registration_details']['username'] = $get_username;  //the actual username typed
			$_SESSION['registration_details']['username_check'] = $username_check;  //does username exist in database already?
			$_SESSION['registration_details']['get_username_check'] = $get_username_check; //did they type anything at all?
			$_SESSION['registration_details']['username_validity_check'] = $username_validity_check;  //is it a valid username?
			
			/* EMAIL */
			$_SESSION['registration_details']['email'] = $get_email;  //the actual email address typed
			$_SESSION['registration_details']['get_email_check'] = $get_email_check;  //Did they type anything?
			$_SESSION['registration_details']['email_check'] = $email_check; //Does it exist in the database?
			$_SESSION['registration_details']['email_validity_check'] = $email_validity_check; //Is it a valid email address?
This is taken from an example site I'm working on. Assume that $get_username and all the other vars are already loaded and declared etc.

Does that help?

Rob
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Thanks Rob,

Yes it helps a little but here is how I was thinking of setting it out. Is it possible to do it this way?

On clicking add to basket I was thinking of something like this.

Code: Select all

<a href="cart_add.php?productid=<php echo ' '.$prodid.' ' ?>&price=<php echo ' '.$price.' ' ?>>"ADD</a>
________________________________________________________________________________________

cart_add.php

Code: Select all

<?php   session_start();

$prodid ='$_GET[prodid]'
$price ='$_GET[price]''

$_SESSION['prodid']=$prodid; //What I'm thinking is, this will overwrite the first product added - How wold I get around this?

$_SESSION['price']=$price;

?>
Thanks for your help.
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

I have just come accross this but I am not sure how to impplement it with my code.

Can anyone help?

Thanks

Code: Select all

session["items"][n][0] = id 
session["items"][n][1] = name 
session["items"][n][2] = price 
session["items"][n][3] = amount
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$prodid = $_GET['prodid'];
$price = $_GET['price'];
Firstly, you cannot parse variables inside second quotes.
Secondly, to add multiple items to a variable you have to define it as an array.
I would organize it somewhat like

Code: Select all

$_SESSION['products'][$prodid] = array('name' => $productname, 'price' => $price);
To access, lets say your product #4.. you would do

Code: Select all

echo $_SESSION['products'][4]['name'];
echo $_SESSION['products'][4]['price'];
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

wow, you can have a session array? That's fantastic.

I think that's about all I can offer, it seems you're in safe hands with Jcart.

Good luck :)

Rob
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

$_SESSION itself is an array, and can be treated just as other variables :wink:
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

Jcart wrote:$_SESSION itself is an array, and can be treated just as other variables :wink:
Nice, I think that will really help the original poster of this thread, and for me, demystifies sessions that little bit more. All I have to figure out now is how to delete / remove a session.

I'll go search php.net ;)
BruceT
Forum Newbie
Posts: 14
Joined: Sat Aug 27, 2005 10:23 am

Post by BruceT »

session_start() and session_destroy()

:)
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

cough.... right ;)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

most of the time I have other session variables that I need to keep intact.
To remove a particular session var you can do things like

Code: Select all

$_SESSION['var'] = array();
//or
unset($_SESSION['var']);
too..
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

so in my past example it would be?

Code: Select all

unset($_SESSION['registration_details']);
BruceT
Forum Newbie
Posts: 14
Joined: Sat Aug 27, 2005 10:23 am

Post by BruceT »

Yep, that's the ticket...
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

Thanks everyone, and I am sorry I hijacked this thread, not something I normallyh do, but I believe the original question was answered (I hope).

Rob
Post Reply