Jump to content

Saving multiple vars in a Session


decessus

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
Link to comment
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.

Link to comment
Share on other sites

  • 2 years later...
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.