PHP Session Variables + Arrays

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
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

PHP Session Variables + Arrays

Post by xterra »

Good evening,
I am creating an array when the page loads. The # of elements is dependent on what is in the database for that user.

This was my first attempt:

Code: Select all

$i=0;
while ($i<$num)
{

session_register("groups_FK[$i]");
//REGISTER groups_FK[#]

$_SESSION['groups_FK[$i]'] = $i;
//make variable:
//groups_FK[#]=#


$i++;
}

//this creates the following in memory:
// groups_FK[0]=#
// groups_FK[1]=#
//..etc
However, PHP does not like me using 'variables' to access elements. It only works if I do this:

Code: Select all

session_register("groups_FK[0]");
$_SESSION['groups_FK[0]'] = 0;
session_register("groups_FK[1]");
$_SESSION['groups_FK[1]'] = 1;
session_register("groups_FK[2]");
$_SESSION['groups_FK[2]'] = 2;
session_register("groups_FK[3]");
$_SESSION['groups_FK[3]'] = 3;
session_register("groups_FK[4]");
$_SESSION['groups_FK[4]'] = 4;
Furthermore, when accessing these session variables from other pages, I cannot access certain elements through a variable, I have to use an actual number.

Any assistance is greatly appreciated.

Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

session_register() is depreciated and you do not need to use it, and

Code: Select all

$_SESSION['groups_FK[$i]'] = $i;
with using single quotes '' the string is not parsed so it is not changing $i to it's intended value.

This may be helpful: http://us.php.net/types.string
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

uhm, why not build the array first and assign it all at once?
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Post by xterra »

Thanks, but it still doesn't work.

I hard coded it again using your suggestion (removing the register).

Code: Select all

$_SESSION['groups_FK[0]'] = 0;

$_SESSION['groups_FK[1]'] = 1;

$_SESSION['groups_FK[2]'] = 2;

$_SESSION['groups_FK[3]'] = 3;
But accessing it still doesn't work

Code: Select all

$i=1;

$var=$_SESSION['groups_FK[$i]'];
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$_SESSION['groups_FK'] = array();
$_SESSION['groups_FK'][0] = 0;
$_SESSION['groups_FK'][1] = 1;
// and so on
this way iteration would work
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Re: PHP Session Variables + Arrays

Post by AKA Panama Jack »

xterra wrote:Good evening,
I am creating an array when the page loads. The # of elements is dependent on what is in the database for that user.

This was my first attempt:

Code: Select all

$i=0;
while ($i<$num)
{

session_register("groups_FK[$i]");
//REGISTER groups_FK[#]

$_SESSION['groups_FK[$i]'] = $i;
//make variable:
//groups_FK[#]=#


$i++;
}

//this creates the following in memory:
// groups_FK[0]=#
// groups_FK[1]=#
//..etc
However, PHP does not like me using 'variables' to access elements. It only works if I do this:

Code: Select all

session_register("groups_FK[0]");
$_SESSION['groups_FK[0]'] = 0;
session_register("groups_FK[1]");
$_SESSION['groups_FK[1]'] = 1;
session_register("groups_FK[2]");
$_SESSION['groups_FK[2]'] = 2;
session_register("groups_FK[3]");
$_SESSION['groups_FK[3]'] = 3;
session_register("groups_FK[4]");
$_SESSION['groups_FK[4]'] = 4;
Furthermore, when accessing these session variables from other pages, I cannot access certain elements through a variable, I have to use an actual number.

Any assistance is greatly appreciated.
First off session_register("groups_FK[$i]"); is no longer needed and your program will not work on servers where register_globals is disabled. Register_globals is disabled by default on all versions from PHP 4.2.0 and up. Read about it here http://us.php.net/function.session_register . You can just assign a value to the session variable array and it is automatically registered. Also, you are not creating an array in the $_SESSION array with that you are doing. Here is the easiest way to accomplish your goal. Just putting brackets around the number doesn't make it an array element in the $_SESSION array. :)

Code: Select all

$i=0;
while ($i<$num)
{
	$session_element = "groups_FK$i";
	$_SESSION[$session_element] = $i;
	//make variable:
	//groups_FK#=#

	$i++;
}

//this creates the following in memory:
// groups_FK0=#
// groups_FK1=#
//..etc

for($i = 0; $i < $num; $i++)
{
	$session_element = "groups_FK$i";
	echo $_SESSION[$session_element] . "<br>";
}
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Post by xterra »

AKA Panama Jack, thank you very much it works perfectly.

I didn't think about doing it like that. Makes me really like PHP the fact that you can dynamically create variables in a string form like that.

Take care,
Robert
Post Reply