Forum Moderators: coopster

Message Too Old, No Replies

Cannot change values of a multi-dimensional Session Array

Do I have to change them by reference?

         

neophyte

11:36 am on Aug 26, 2007 (gmt 0)

10+ Year Member



Hello All -

Big problem I've been struggling with for almost 2 days:

I'm trying to build a "blind" validation routine that will take any kind of form elements and validate them for empty as well as varying content. This routine is called on submit and is based upon data stored in a number of multi-dimensional $_Session arrays like this:

$_SESSION['TSets'] = array
(
'Personal'
);

//Sessions Arrays for Personal Information
$_SESSION['Personal'] = array
(
'first' => array
(
'name' => 'first',
'type' => 'text',
'value' => NULL,
'validate' => TRUE,
'empty' => FALSE,
'invalid' => FALSE,
'hcode' => FALSE,
'function' => 'name()',
),
'address' => array
(
'name' => 'address',
'type' => 'text',
'value' => NULL,
'validate' => TRUE,
'empty' => FALSE,
'invalid' => FALSE,
'hcode' => FALSE,
'function' => 'address()',
)
)

These arrays reside on the form page which, when submitted, pulls in the validation routine (some of it shown here):

if($_SESSION['TSets']!= NULL) $processText = TRUE;

//if($_SESSION['CbSets']!= NULL) $processCheck = TRUE;

if($processText)
{
//Loop through each set array
foreach($_SESSION['TSets'] as $key => $value)
{
//Transfer the name of each set into $set
$set = $_SESSION['TSets'][$key];

//Loop through each field array
foreach($_SESSION[$set] as $field => $value)
{
//Transfer the current field name, type, and function into local VARs
$name = $_SESSION[$set][$field]['name'];
$type = $_SESSION[$set][$field]['type'];
$function = $_SESSION[$set][$field]['function'];
$validate = $_SESSION[$set][$field]['validate'];

//Transfer $_POST into 'value', and into the local VAR $value
$value = $_SESSION[$set][$field]['value'] = $_POST[$name];

switch($type)
{
case 'text':
$_SESSION[$set][$field]['empty'] = textEmpty($value);
break;

case 'select':
$_SESSION[$set][$field]['empty'] = selectEmpty($value,$selectErrors);
break;
}

if($_SESSION[$set][$field]['empty'] == TRUE)
{
$_SESSION[$set]['set']['empty'] = TRUE;
continue;
}
}
}

I'm not getting any errors when the script runs, but when I try to assign one of the sessions a new value, nothing happens - If I manually input different values ON THE VALIDATION PAGE for testing purposes like this:

$_SESSION[$set][$field]['empty'] = 'something';

... which is literally $_SESSION['Personal']['value']['empty']... the new value WILL echo from the validation page, but it won't echo when the form page re-displays. Very strange to me.

The only thing that I can think of is that somehow the $_Sessions are becoming duplicated (?) and the REAL session are left unaffected?

Or, it may just be that I'm screwing something else up entirely. I didn't think that what I'm trying to achieve is over my head (technically) but it appears to be so. The only thing that comes to my mind is that I may to change the array elements within these sessions by reference but I've never tried that before and don't see why I'd need to - after all it's "just" a multi-dimensional array within a $session - that's my simple mind talking.

Would someone be so kind as to take a look at the above code and see what I may be doing wrong (this time).

Appreciation in advance,

Neophyte

eelixduppy

3:03 pm on Aug 30, 2007 (gmt 0)



changing the value manually like that should work. Are you sure you are starting the session somewhere at the top of the script?:

session_start();

I can't really think of anything else that would cause that behavior...

neophyte

11:37 am on Aug 31, 2007 (gmt 0)

10+ Year Member



eelixduppy --

Thanks for your reply; The session is set so I know that isn't the problem. I've stripped down my code and reposted the question so it will (hopefully) be a little clearer... however, after these many days trying to work this out I'm not at all sure how clear anything is to me any more!

I've never stepped into multi-dimensional arrays before and OMG is it giving me heartaches. I think I've got my session addressing messed up (perhaps incomplete) but I'm not sure. If you'd like to take a look at my re-post I'd appreciate your - or anyone else's - guidance.

jatar_k

12:16 pm on Aug 31, 2007 (gmt 0)