Page 1 of 1
Session for products
Posted: Tue Sep 06, 2005 6:16 pm
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.
Posted: Tue Sep 06, 2005 6:21 pm
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
Posted: Tue Sep 06, 2005 6:36 pm
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.
Posted: Tue Sep 06, 2005 6:47 pm
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
Posted: Tue Sep 06, 2005 6:50 pm
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'];
Posted: Tue Sep 06, 2005 6:57 pm
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
Posted: Tue Sep 06, 2005 7:35 pm
by John Cartwright
$_SESSION itself is an array, and can be treated just as other variables

Posted: Tue Sep 06, 2005 7:40 pm
by robster
Jcart wrote:$_SESSION itself is an array, and can be treated just as other variables

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

Posted: Tue Sep 06, 2005 7:53 pm
by BruceT
session_start() and session_destroy()

Posted: Tue Sep 06, 2005 7:58 pm
by robster
cough.... right

Posted: Tue Sep 06, 2005 8:08 pm
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..
Posted: Tue Sep 06, 2005 8:13 pm
by robster
so in my past example it would be?
Code: Select all
unset($_SESSION['registration_details']);
Posted: Tue Sep 06, 2005 8:50 pm
by BruceT
Yep, that's the ticket...
Posted: Wed Sep 07, 2005 12:25 am
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