Currently I have a number of html forms which loop through records in
a database table. In order to be able to update the right record when
the form is submitted, I've tacked number on to the field names, i.e.:
PHP: echo '<input type="text" name="hat_color_'.$i.'"
value="'.$currentRow['hat_color'].'">
<input type="text" name="favorite_food_'.$i.'"
value="'.$currentRow['favorite_food'].'">';
HTML: <input type="text" name="hat_color_0" value="brown"><input
type="text" name="favorite_food_0" value="pizza">
<input type="text" name="hat_color_1" value="black"><input type="text"
name="favorite_food_1" value="fries">
....
After submit, $_POST['hat_color_0'] = 'brown'
$_POST['hat_color_1'] = 'black'
$_POST['favorite_food_0'] = 'pizza'
$_POST['favorite_food_1'] = 'fries'
....
In the past I have also done this with arrays, i.e.:
PHP: echo '<input type="text" name="hat_color[]"
value="'.$currentRow['hat_color'].'">';
HTML: <input type="text" name="hat_color[]" value="brown">
<input type="text" name="hat_color[]" value="black">
....
After submit, $_POST['hat_color'][0] = 'brown'
$_POST['hat_color'][1] = 'black'
....
Now, here's my crazy idea... Since it's really easy to get my query
results into an array in the form of
$result_array[$rownumber][$fieldname], would I run into any problems
naming my form fields like this:
HTML: <input type="text" name="0[hat_color]" value="brown">
<input type="text" name="0[favorite_food]" value="pizza">
<input type="text" name="1[hat_color]" value="brown">
<input type="text" name="1[favorite_food]" value="pizza">
....
After submit, $_POST[0]['hat_color'] = 'brown'
$_POST[0]['favorite_food'] = 'pizza'
$_POST[1]['hat_color'] = 'black'
$_POST[1]['favorite_food'] = 'fries'
....
I know that it's fairly easy to switch back and forth... It just
struck me as a way to keep all my arrays in the same form whether I'm
getting data from the database, an html form, or whatever. Using
name="123[yadda_yadda]" strikes me as odd, but it seems to work.
Anyone got a good reason not to do this?