I have a PHP form that pulls data from the database, prints it out in rows, and then allows the user to check which items they need. On save, only the very last option is being sent through to $_POST. Here's my code:
global $dbc;
$afQuery = "SELECT fid, facility_name, city, state, country, phone from facilities
WHERE uid = '123'
AND fid != '456'
GROUP BY fid;";
$afResult = $dbc->query($afQuery,__FILE__,__LINE__);
$i = 0;
while ($affiliated = $dbc->fetch($afResult)) {
++$i;
echo '<div class="action" style="float:left;">';
echo '<input type="checkbox" name="affiliated[afid]" id="afid" value="'.$affiliated->fid.'">';
echo '</div>';
echo '<div class="facility-name">';
echo '<input type="hidden" id="facility-name '.$i.'" name="affiliated[facility_name]" value="'.$affiliated->facility_name.'" />';
echo $affiliated->facility_name;
echo '</div>';
echo '<div class="facility-location">';
echo '<input type="hidden" id="facility-location '.$i.'" name="affiliated[facility_location]" value="'.$affiliates->city. ', ' . $affiliated->state . ' ' . $affiliated->country.'" />';
echo $affiliated->city. ', ' . $affiliated->state . ' ' . $affiliated->country;
echo '</div>';
echo '<div class="facility-phone">';
echo '<input type="hidden" id="facility-phone '.$i.'" name="affiliated[facility_phone]" value="'.$affiliated->phone.'" />';
echo $affiliated->phone;
echo '</div>';
echo '<div class="clear"></div>';
echo '<input type="hidden" id="update" name="update" value="' . $affiliated->fid . '" />';
}
$dbc->fetch is equivalent to mysql_fetch_assoc.
I am trying to save these values to the $affiliated array, but it should have sub-arrays for each selected item, so I can store each value in the database on a separate row. Where am I missing it? I tried a foreach within the while loop, but I got nowhere with that.
Thanks for your help!
* UPDATE *
I have followed @Pinetree's suggestion and added $i like so:
<input type="checkbox" name="affiliated[afid]['.$i.']" id="afid" value="'.$affiliated->fid.'">
However, now I receive the following output:
Array
(
[afid] => Array
(
[1] => 5289
[2] => 5290
[3] => 5291
[4] => 5292
)
[facility_name] => Array
(
[1] => Test Company
[2] => Test Company 2
[3] => Test Company 3
[4] => Test Company 4
)
[facility_location] => Array
(
[1] => Address, State US
[2] => Address, State US
[3] => Address, State US
[4] => Address, State US
)
[facility_phone] => Array
(
[1] => 555-555-5555
[2] => 555-555-5555
[3] => 555-555-5555
[4] => 555-555-555
)
)
I need this to instead look like this so I can insert each complete row into a table in the database:
Array
(
[0] => Array
(
[afid] => 5289
[facility_name] => Test Company
[facility_location] => Address, ST USA
[facility_phone] => 555-555-5555
)
[1] => Array
(
[afid] => 5290
[facility_name] => Test Company 1
[facility_location] => Address, ST USA
[facility_phone] => 555-555-5555
)
)
Suggestions?
heredocsyntax when echoing out big chunks of HTML like that: php.net/manual/en/…