Forum Moderators: coopster

Message Too Old, No Replies

Multidimensional Angst

I think it's the set-up, but I just can't get it right

         

neophyte

11:07 am on Aug 25, 2007 (gmt 0)

10+ Year Member



Hello All -

Here's a brief backgrounder:

I'm trying to create a multi-dimensional array which holds two types of information: form sets and form element information.

The form sets contain things like the set name, legend text, whether it should be validated, etc.

The element part contains info about each form element within each set. There could potentially be any number of "set arrays" which could potentially contain any number of individual "form element arrays".

After almost 12 hours of experimentation, my array still isn't working and I can't see why. Here's what I've got so far:

The Arrays:

$_SESSION['TextSets'] = array
(
0 => array // (FIRST SET ARRAY): PERSONAL
(
'setname' => 'personal',
'setvalidate' => TRUE,
'seterror' => FALSE,
'setlegend' => 'Personal Details:',
),
0 => array // (FIRST ELEMENT ARRAY WITHIN THE FIRST SET): PERSONAL>FIRST
(
'name' => 'first',
'type' => 'text',
'value' => NULL,
'validate' => TRUE,
),
1 => array // (SECOND ELEMENT ARRAY WITHIN THE FIRST SET): PERSONAL>LAST
(
'name' => 'last',
'type' => 'text',
'value' => NULL,
'validate' => TRUE,
),

1 => array // (SECOND SET ARRAY): CONTACT
(
'setname' => 'contact',
'setvalidate' => TRUE,
'seterror' => FALSE,
'setlegend' => 'Contact Information:',
),
0 => array // (FIRST ELEMENT ARRAY WITHIN THE SECOND SET): CONTACT>PHONE
(
'name' => 'phone',
'type' => 'text',
'value' => NULL,
'validate' => TRUE,
)
);

...and the last of my many attempts to test-output the array:

if($_SESSION['TextSets']!= NULL)
{

for($i=0; $i < count($_SESSION['TextSets'][$i]); $i++)
{
echo $_SESSION['TextSets'][$i]['setname'] . "<br>";
echo $_SESSION['TextSets'][$i]['setlegend'] . "<br>";

for($j=0; $j < count($_SESSION['TextSets'][$j]); $j++)
{
echo $_SESSION['TextSets'][$i][$j]['name'] . "<br>";
echo $_SESSION['TextSets'][$i][$j]['type'] . "<br>";
echo $_SESSION['TextSets'][$i][$j]['validate'] . "<br>";
}
}
}

Either my array set-up is incorrect, or the way I'm trying to address the individual arrays and keys are incorrect or (probably), both.

If someone could show me the error(s) of my way's I would be - as always - very greatful.

Neophyte

jatar_k

12:06 pm on Aug 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the first thing I noticed is that you are overwriting your array elements, you are only assigning to 2 elements, 1 and 0

try this

<edit>wait, now I see what the problem is, I removed my last bit here and I will see if I can fix it

jatar_k

12:23 pm on Aug 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you weren't going deep enough, so your elements in each set were just overwriting your sets at 0 and 1. I changed the structure a bit becaus eI understand the way you were going to do it but I think it becomes too confusing. Hopefully this makes sense. I left in the testing/viewing code so you could look at the structure.

session_start();

$_SESSION['TextSets'] = array (
0 => array (
'setname' => 'personal',
'setvalidate' => TRUE,
'seterror' => FALSE,
'setlegend' => 'Personal Details:'
),
1 => array (
'setname' => 'contact',
'setvalidate' => TRUE,
'seterror' => FALSE,
'setlegend' => 'Contact Information:'
)
);

$_SESSION['TextSets']['personal'] = array (
0 => array
(
'name' => 'first',
'type' => 'text',
'value' => NULL,
'validate' => TRUE,
),
1 => array
(
'name' => 'last',
'type' => 'text',
'value' => NULL,
'validate' => TRUE,
)
);

$_SESSION['TextSets']['contact'] = array (
0 => array
(
'name' => 'phone',
'type' => 'text',
'value' => NULL,
'validate' => TRUE,
)
);

echo '<pre>';
print_r($_SESSION);
echo '</pre>';

echo '<p>now the form<p>';

if($_SESSION['TextSets']!= NULL) {
for($i=0; $i < count($_SESSION['TextSets'][$i]); $i++) {
$setname = $_SESSION['TextSets'][$i]['setname'];
echo $setname . "<br>";
echo $_SESSION['TextSets'][$i]['setlegend'] . "<br>";
for($j=0; $j < count($_SESSION['TextSets'][$setname]); $j++) {
echo $_SESSION['TextSets'][$setname][$j]['name'] . "<br>";
echo $_SESSION['TextSets'][$setname][$j]['type'] . "<br>";
echo $_SESSION['TextSets'][$setname][$j]['validate'] . "<br>";
}
}
}

neophyte

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

10+ Year Member



jatar_k -

Thank you so much for your help. The numeric array was driving me crazy trying to remember what numbers represented which array so I gave up on that. I think that I've solved my problem with the multi-dimensional issue - I've broken out sets AND field arrays into seperate sessions - simular to your example, but I'm not re-combining them. Now that things "appear" to be working, I've got another headache regarding trying to change the values of the arrays within each session.

If you'd like to weigh-in on this issue, the thread is titled: Cannot change values of a multi-dimensional Session Array.

Thanks again for all of your previous guidance.

Neophyte