arrays in $_SESSION
Posted: Sun Nov 17, 2002 11:12 am
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:
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:
It looks like there is something I fundamentally do not understand about arrays in $_SESSION. Can anyone point me in the right direction here?
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];
}
}
?>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
?>