Click to See Complete Forum and Search --> : array
paperbox005
10-08-2004, 06:57 PM
hello,
could someone give me some code or something because i've done a bit of searching but its all very confusing
im trying to store an array in a session variable so that i can use across a few pages.
how do i add a empty array to the session?
and can i update the array at any point in my code?
<?php
session_start();
$_SESSION["obj"] = array("A ", "string ", "array.");
foreach($_SESSION["obj"] as $obj){
print $obj;
}
# ...
$_SESSION["obj"][0] = "The ";
foreach($_SESSION["obj"] as $obj){
print $obj;
}
?>
paperbox005
10-08-2004, 08:07 PM
ok cool i think i get that,
i was wondering if i could do it this way tho
$_SESSION['array1'] = Array();
$_SESSION['array2'] = Array();
$_SESSION['array3'] = Array();
.
.
.
$arr1 = $_SESSION['array1'];
$arr2 = $_SESSION['array2'];
$arr3 = $_SESSION['array3'];
$arr1 = array($a, $b, $c, $d);
$arr2 = array($e);
$arr3 = array($f);
.
.
.
$a = $arr1[0];
$b = $arr1[1];
$arr3[] = $x; //append to the end of array?
oh and is there way to check if the array is empty ?
Yes, you can treat session variables just like any other variable. They just have an added purpose. ;)
$arr = array(2, 1, 4);
$arm = array();
if(empty($arr)){
echo '$arr is empty.';
}
if(empty($arm)){
echo '$arm is empty.';
}
paperbox005
10-08-2004, 10:33 PM
cool stuff
last question
where exactly do i have to declare new session variables?
//declaring 3 arrays
$_SESSION['array1'] = Array();
$_SESSION['array2'] = Array();
$_SESSION['array3'] = Array();
i have 3 pages which will be using these arrays, and im not sure in which page i'll have to actually put this code.
Put it on the first page that should occur. If there is none which comes first every time, check to see if they exist; if they do not, create them.
if(empty($_SESSION['array1']) || empty($_SESSION['array2'])
|| empty($_SESSION['array3'])) {
$_SESSION['array1'] = array();
$_SESSION['array2'] = array();
$_SESSION['array3'] = array();
}
paperbox005
10-08-2004, 11:04 PM
cool stuff,
getting the hang of this
but one last thing
i have something like this where the arrow means on submit goto
page1->page2->page3->page2->page3->page4
and on page2 i am putting things into the array.
next time when i reach page2 i want to check if there are items in the array which of course there is now.. but somehow its not picking up these items..
$arr1 = $_SESSION['array1'];
$arr2 = $_SESSION['array2'];
$arr3 = $_SESSION['array3'];
#...
$count = count($arr1);
if($count == 0)
{
$arr[0] = $a;
$arr[1] = $b;
$arr[2] = $c;
$arr[3] = $d;
echo "hello";
}
now on the first time i access page2, it prints "hello" and on the second time it shouldnt print the "hello" but it does.. ?
That's due to bad programming practice. Add this line to the beginning of that PHP file.
error_reporting(E_STRICT | E_ALL);
paperbox005
10-08-2004, 11:14 PM
hmm, thats to catch errors right?
but my php script isnt returning any errors, its still working its just not registering that i've added things to the array already.. it seems to add and then discard?
A session will remain set if you leave the page and return to it again; the session will have to be destroyed for them to all be unset.
Paul Jr
10-09-2004, 12:15 AM
Originally posted by paperbox005
it seems to add and then discard?
Are you sure you have called session_start(); (http://www.php.net/session-start) on each page in which you want to use sessions?