Jump to content


Photo

Saving multiple vars in a Session


  • Please log in to reply
2 replies to this topic

#1 decessus

decessus
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 09 March 2006 - 08:10 AM

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

#2 lessthanthree

lessthanthree
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationUK

Posted 09 March 2006 - 09:34 AM

[!--quoteo(post=353183:date=Mar 9 2006, 08:10 AM:name=decessus)--][div class=\'quotetop\']QUOTE(decessus @ Mar 9 2006, 08:10 AM) View Post[/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...

<?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;

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


call me a safe bet, i'm betting i'm not

#3 bahewitt

bahewitt
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 01 January 2009 - 10:23 AM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users