Page 1 of 1

Nested Post Variable Arrays

Posted: Tue Jul 09, 2002 12:08 pm
by Junk Guy
Hello. I have a situation and was wanting some input on it.

I have a form that uses arrays for the inputs:
...
print "<INPUT TYPE='HIDDEN' NAME='id[]' VALUE='".OCIResult($stmt,1)."'>\n";
...

This is because I have no idea how many of these rows I will submit for update. Anyway, this approach works fine when I set my form action to submit to another page, which then does the updates and redirects to my original page. So I know the code as it stands works.

However, when I try to put this "form handler" into a function so I can distribute it amongst multiple pages, I run into problems when trying to utilize the form elements.

My form handler call is as such:
handle_form($HTTP_POST_VARS)

The function declaration is as such:
handle_form($form_vars)

Due to this arrangement, I seemingly have to handle the POST vars I passed in as:
$form_vars['key']

But this doesn't work with the form/input arrays I'm using:
$form_vars['$id[$count]']

and I can't access the values if I just use
$id[$count]

Can anybody tell me what I'm missing?

Thanks for your time.

Posted: Tue Jul 09, 2002 12:17 pm
by hob_goblin
have you looked into foreach()?

Posted: Tue Jul 09, 2002 12:23 pm
by Junk Guy
Not really, mainly because I have a mix of form items being returned.

Some have only one instance, others may have N instances.

The example input I showed would be one of the N instance ones, which is why I am using the array. And there are multiple arrays within the form, as well.

Here's a glimpse:

...
while()
{
print "<INPUT TYPE='HIDDEN' NAME='savedesc[]' VALUE='".OCIResult($stmt,2)."'>\n";
print "<INPUT TYPE='HIDDEN' NAME='id[]' VALUE='".OCIResult($stmt,5)."'>\n";
print "<INPUT TYPE='HIDDEN' NAME='id2[]' VALUE='".OCIResult($stmt,7)."'>\n";
}
print "<INPUT TYPE='HIDDEN' NAME='TRACK' VALUE='".OCIResult($stmt,6)."'>\n";
...

Posted: Tue Jul 09, 2002 12:44 pm
by hob_goblin
http://www.php.net/is_array

then you could just use an if() statement to tell if it has more than 1 instance...

Posted: Tue Jul 09, 2002 1:18 pm
by Junk Guy
How am I going to be able to tell if something is an array if I can't get to it?

IE:
$form_vars['$id[$count]']

from my first post.

Posted: Tue Jul 09, 2002 2:28 pm
by hob_goblin
well since formvars is an array

do like

Code: Select all

foreach($formvars as $key)&#123;
if(is_array($key))&#123;
//do stuff with the arrays
&#125; else &#123;
//do stuff with the strings
&#125;
&#125;