Forum Topic: Post Arrays Loop Php Help

(174 views • 6 replies)

This topic is 1 page long.

<< < > >>
Resigned

ZiggyZack99

Reply To Post Reply & Quote

Posted at: 12/24/08 05:15 PM

ZiggyZack99 DARK LEVEL 15

Sign-Up: 04/15/07

Posts: 2,780

OK, so let's get ourselves a hypothetical situation going here. Let's say I had this javascript on a page, that whenever a person clicked a button, this would be added to the page:
<input name=data[i] type=text>
Where i=the iteration of the button being clicked. So we would have:
<input name=data[0] type=text>
<input name=data[1] type=text>
<input name=data[2] type=text>
<input name=data[3] type=text>
After a while. Then I submit it to PHP. My question is: how would I process each field individually with a loop, no matter how many fields there are? Like, I could echo each value, or add it to a database. How would this be done?


None

polym

Reply To Post Reply & Quote

Posted at: 12/24/08 06:28 PM

polym LIGHT LEVEL 10

Sign-Up: 10/02/07

Posts: 801

well since it's in a array all you have to do is manipulate the array. since it's an arbritrary size you can have a hidden input form.

so with the javascript whenever the iterator increments,

you change the hidden input's form's value.

and then just pull the hidden input in PHP.


None

polym

Reply To Post Reply & Quote

Posted at: 12/24/08 06:30 PM

polym LIGHT LEVEL 10

Sign-Up: 10/02/07

Posts: 801

example of how this is done.

<input type="hidden" id="counter" />

$counter = $_POST['counter'];
for ($i = 1; $i < $counter; $i++)
 echo $_POST['whatever' . $i);

Resigned

ZiggyZack99

Reply To Post Reply & Quote

Posted at: 12/24/08 06:58 PM

ZiggyZack99 DARK LEVEL 15

Sign-Up: 04/15/07

Posts: 2,780

At 12/24/08 06:30 PM, polym wrote: example of how this is done.

<input type="hidden" id="counter" />

$counter = $_POST['counter'];
for ($i = 1; $i < $counter; $i++)
echo $_POST['whatever' . $i);

I see... thanks!


None

DFox

Reply To Post Reply & Quote

Posted at: 12/25/08 01:10 AM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,474

That's not really how you want to do it.

There is no reason to have any sort of counter, and you aren't doing the HTML elements correctly.

Don't define the index in the fields, you could just do this and they will be automatically defined:

<input type="text" name="data[]"><br/>
<input type="text" name="data[]"><br/>
<input type="text" name="data[]"><br/>
<input type="text" name="data[]"><br/>

Then, you're PHP logic is off. Just count the elements in the $data array in the POST array. Like so:

<?php
$num_fields = count($_POST['data']);
for ($i = 0; $i < $num_fields; $i++)
{
	echo $_POST['data'][$i] . '<br/>';
}
?>

This will get you started, but if you're actually using this code in a production environment you need to do things like error checking and making sure $_POST['data'] is actually an array (is_array).


None

henke37

Reply To Post Reply & Quote

Posted at: 12/25/08 04:17 AM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,590

Why no love for the foreach loop?

<?php
foreach($_POST['field'] as $element) {
  echo htmlentities($element);
}

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


Resigned

ZiggyZack99

Reply To Post Reply & Quote

Posted at: 12/25/08 01:21 PM

ZiggyZack99 DARK LEVEL 15

Sign-Up: 04/15/07

Posts: 2,780

At 12/25/08 01:10 AM, DFox wrote: That's not really how you want to do it.

It is.

There is no reason to have any sort of counter, and you aren't doing the HTML elements correctly.

Well...

Don't define the index in the fields, you could just do this and they will be automatically defined:

Good point.

Then, you're PHP logic is off. Just count the elements in the $data array in the POST array. Like so:

Ah, phuck...

This will get you started, but if you're actually using this code in a production environment you need to do things like error checking and making sure $_POST['data'] is actually an array (is_array).

It's not really "production environment", per se, but along with everything, I guess it is... thanks!


All times are Eastern Standard Time (GMT -5) | Current Time: 12:58 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!