Page 1 of 1

arrays in $_SESSION

Posted: Sun Nov 17, 2002 11:12 am
by Bill H
Here's my plan. I want to open a data file and load the data when the user logs on, then update it when he clicks an "Update" button. In between I want him to add/modify/delete elements of an array which are not recorded to the data file until he clicks the "Update" button and are discarded if he leaves without clicking it. But that discard needs to persist if he returns during the same browser session. I have all of it working except for the array.

Here's how I load the array:

Code: Select all

<?php
unset($_SESSIONї'names']);              
// make sure nothing left over from previous login during this session

$r = "";
$f = @fopen("files/acode.dat", "r");          // open names/codes file
if ($f != FALSE)
{    $a = @fread($f, filesize("files/acode.dat"));
       fclose($f);
       $r = trim($a);                           // trim any excess
       $adata = explode("|", $r);                 // array of names
       for ($i = 0; $i < count($adata); $i++)
       {    $_SESSIONї'namesї$i]'] = $adataї$i];
       }
}
?>
I printed out the array of names ($adata) and it is complete with all the names. But when I print_r($_SESSION['name']) i get nothing.

During the session I add to the array as follows:

Code: Select all

<?php
$txt1 = $_POSTї'NewNam'];
if (strlen($txt1)  == 4)
{    $_SESSIONї'namesї]'] = $txt1;
}
// it was not added, so I tried
$txt1 = $_POSTї'NewNam'];
if (strlen($txt1)  == 4)
{    $i = 0;
       while(isset($_SESSIONї'namesї$i]']))
            $i++;
       $_SESSIONї'namesї$i]'] = $txt1;
}
// but it timed out on the "while" line
?>
It looks like there is something I fundamentally do not understand about arrays in $_SESSION. Can anyone point me in the right direction here?

Posted: Sun Nov 17, 2002 12:10 pm
by volka
try $_SESSION['names'][$i] instead of $_SESSION['names[$i]']

Posted: Sun Nov 17, 2002 2:25 pm
by Bill H
Thanks. Your reply indicates that I was correct in the following statement.
there is something I fundamentally do not understand about arrays in $_SESSION
However, the following:

Code: Select all

<?php
$adata = explode("|", $dї0]);                 // array of names
for ($i = 0; $i < count($adata); $i++)
{    $_SESSIONї'names']ї$i] = $adataї$i];
}
?>
Still leaves no list of names in $_SESSION, and this:

Code: Select all

<?php
if (strlen($txt1) == 4)
{    $_SESSIONї'names']ї] = $txt1;
}
?>
does not add an element.

Posted: Mon Nov 18, 2002 3:04 am
by twigletmac
Do you have session_start() at the top of the script?

Mac

Posted: Mon Nov 18, 2002 8:19 am
by Bill H
Yes, I do. The other S_SESSION variables, ones that aren't arrays, work as expected.

Posted: Mon Nov 18, 2002 6:28 pm
by Bill H
This works. [Using strlen() instead of isset().]
Don't know why, but....
Volka, your help was immensly valuable.

Code: Select all

<?php
          if (strlen($txt1) && strlen($txt2) == 4)
          {    $i = 0;
               while(strlen($_SESSIONї'names']ї$i])) $i++;
               $_SESSIONї'names']ї$i] = $txt1;
               $_SESSIONї'codes']ї$i] = $txt2;
          }
?>