Page 1 of 1

putting an array directly into a $_SESSION from an include

Posted: Tue Sep 02, 2003 4:49 am
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

Posted: Tue Sep 02, 2003 6:06 am
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));

Posted: Tue Sep 02, 2003 6:42 am
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

Posted: Tue Sep 02, 2003 8:11 am
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

Posted: Tue Sep 02, 2003 8:43 am
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:

Posted: Tue Sep 02, 2003 9:05 am
by twigletmac
And don't forget to quote your attribute names:
Array do's and don'ts - Why is $foo[bar] wrong?

Mac