putting an array directly into a $_SESSION from an include

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
VisionsOfCody
Forum Commoner
Posts: 30
Joined: Sat Mar 01, 2003 1:19 pm
Location: France

putting an array directly into a $_SESSION from an include

Post by VisionsOfCody »

hi again - more array traumas

when my page opens it 'loads' two arrays from an included file which has this in it :

$title = array("this is title 1", "this is title 2", "this is title 3", "this is title 4");
$text = array("this is text 1", "this is text 2", "this is text 3", "this is text 4");

ok - this happens the first time, but after that i need to add/remove elements from the arrays and only save it back to a file right at the end.
So, I figured i'd use a SESSION variable to contain each array and that way i could keep it each time the page is reloaded.

I read the manual section and the 'Stcky' on global session variables but i must admit i didn't really see how to turn my arrays into session variables.

do i just do this? :

$_SESSION[test] = $test;

and how do i access the array elements - is it like this ?:

Code: Select all

<?php
for($i=0; $i<$numEvents; $i++){
echo $_SESSION[test[$i]]."<br>";
}
?>
well, it won't work and i'm a bit a lost .... :(

could anyone lend a helping hand ?

thanks
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

You could use something like the below (serialize, base64_code):

Code: Select all

// Snippet from the manual.
$some_array = array([...]);
$var_querystring = base64_code(serialize($some_array));
// set the session here with $var_querystring
$some_other_array = unserialize(base64_decode($var_querystring));
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

JAM's method works without sessions. If you want to stick with sessions take a look at this example

Code: Select all

<?php
$arr = array(
		array(1, 2, 3, 4),
		array('a', 'b', 'c', 'd')
	);
	
echo $arr[0][2], "\n";

foreach($arr[1] as $key=>$value)
	echo $key, ':', $value, " \n";
?>
$_SESSION can be treated as a normal array.
So if you've assigned something like

Code: Select all

<?php
$_SESSION['title'] = array("this is title 1", "this is title 2", "this is title 3", "this is title 4");
$_SESSION['text'] = array("this is text 1", "this is text 2", "this is text 3", "this is text 4"); 
?>
you can access it (e.g.) with

Code: Select all

<?php
foreach($_SESSION['text'] as $oneText)
	echo $oneText, " \n";
?>
you might also assign an array one level deper than that

Code: Select all

<?php
$_SESSION['article'] = array(
		array('title'=>'this is title 1', 'text'=>'this is text 1'),
		array('title'=>'this is title 2', 'text'=>'this is text 2'),
		array('title'=>'this is title 3', 'text'=>'this is text 3'),
		array('title'=>'this is title 4', 'text'=>'this is text 4')
	);
?>

Code: Select all

<?php
foreach($_SESSION['article'] as $position=>$article)
	echo '[', $position, ':', $article['title'], ':', $article['text'], "] \n";
?>
http://www.php.net/manual/en/language.types.array.php will tell you more
VisionsOfCody
Forum Commoner
Posts: 30
Joined: Sat Mar 01, 2003 1:19 pm
Location: France

Post by VisionsOfCody »

thanks, that looks very promising - if i define all my session variables in the included file it should be just like they were defined in my main page ?
that way i can re-write the included file at the end with the updated variables.

sounds just what i need
thanks again for your help :D
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

You were close in your original post, but rather than

Code: Select all

echo $_SESSION&#1111;test&#1111;$i]]."<br>";
use

Code: Select all

echo $_SESSION&#1111;test]&#1111;$i]."<br>";
:wink:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

And don't forget to quote your attribute names:
Array do's and don'ts - Why is $foo[bar] wrong?

Mac
Post Reply