Page 1 of 1
PHP Session Variables + Arrays
Posted: Wed May 02, 2007 7:03 pm
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.
Posted: Wed May 02, 2007 7:06 pm
by Z3RO21
session_register() is depreciated and you do not need to use it, and
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
Posted: Wed May 02, 2007 7:14 pm
by d3ad1ysp0rk
uhm, why not build the array first and assign it all at once?
Posted: Wed May 02, 2007 7:32 pm
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]'];
Posted: Wed May 02, 2007 7:49 pm
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
Re: PHP Session Variables + Arrays
Posted: Wed May 02, 2007 7:56 pm
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>";
}
Posted: Wed May 02, 2007 8:33 pm
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