Jump to content

Archived

This topic is now archived and is closed to further replies.

decessus

Saving multiple vars in a Session

Recommended Posts

Hi guys,

I am working on a webshop application and I kind of got stuck making my shopping cart. I want to save the products users order by using sessions. Once the user clicked 'order', 2 vars should be saved, one called 'id' and the other called 'count'. ID will be read out of $_GET['id'], and count will have a standard number, '1'. (Users will be able to change this amount later).

I have no clue how to save multiple vars in a session, and later print em. I think I have to use a for loop, but since I am pretty new at using sessions; and most of the stuff I read is either outdated or wrong (might be me though ;)), I ask you for your help.

Thanks in advance,

-Dec

Share this post


Link to post
Share on other sites
[!--quoteo(post=353183:date=Mar 9 2006, 08:10 AM:name=decessus)--][div class=\'quotetop\']QUOTE(decessus @ Mar 9 2006, 08:10 AM) [snapback]353183[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hi guys,

I am working on a webshop application and I kind of got stuck making my shopping cart. I want to save the products users order by using sessions. Once the user clicked 'order', 2 vars should be saved, one called 'id' and the other called 'count'. ID will be read out of $_GET['id'], and count will have a standard number, '1'. (Users will be able to change this amount later).

I have no clue how to save multiple vars in a session, and later print em. I think I have to use a for loop, but since I am pretty new at using sessions; and most of the stuff I read is either outdated or wrong (might be me though ;)), I ask you for your help.

Thanks in advance,

-Dec
[/quote]

To register session variables you need to add values to the $_SESSION supergobal.

To do this...

[code]
<?php
session_start(); //this must go at the top of every page you need to use the vars on BEFORE even html code)

//put the values into the session vars
$_SESSION["id"] = $id;
$_SESSION["count"] = 1;
[/code]

You can then access the values at any time by treating th $_SESSION supergobal as you would any other array.

Share this post


Link to post
Share on other sites
Hi Dec

I had a similar problem with a blog site I'm developing.

My URL was something like:
edit_post.php?post_id=9&action=add

The submit button was set to:
edit_post.php

But when you reload the page the variables were lost as the new page was loaded.

If you want to have the variables available on an additional page you can assign them to a variable within your script, in my case I assigned $post_id=$_GET['post_id];

Then on the submit button set action to:
edit_post.php?post_id=<?php echo $post_id?>&action=add

And you get to carry your variable assigned to the value through the page.

Hope this helps, and if there is a better way I would love to hear about it

Regards
Bruce

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.