Nested Post Variable Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Junk Guy
Forum Newbie
Posts: 9
Joined: Thu Jun 20, 2002 11:55 am

Nested Post Variable Arrays

Post 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.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

have you looked into foreach()?
Junk Guy
Forum Newbie
Posts: 9
Joined: Thu Jun 20, 2002 11:55 am

Post 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";
...
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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...
Junk Guy
Forum Newbie
Posts: 9
Joined: Thu Jun 20, 2002 11:55 am

Post 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.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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;
Post Reply