I have some data in a html table which has been populated from a mysql database. To put the data into the html table I have done the following:
$meterdetail = getMeterDetails($ticketNumber);
This gets a number of records from the mysql table Then for each record retrieved from the db I do the following..
foreach ($meterdetail as $v2) {
<tr>
<td class='tableText'>
<input type='hidden' name='ids[]' value='".$v2['id']."'>
<input type='text' name='meters[]' value=''>
</td>
</tr>
Ultimately what I am trying to do here is update each record (based on the ID from the mysql table) with the values in the input of the table.
When I click on a submit button I need to step through each record that was collected and insert the value that was typed into the html form corresponding to the relevant id.
To process the $_POST data I do the following
if (!empty($_POST['meters']) && !empty($_POST['ids'])) {
for ($i = 0; $i < count($_POST['meters']); $i++) {
updateRecord($_POST['ids'][$i],$_POST['meters'][$i]);
}
}
This seems to be working but for some reason I don't feel comfortable with this solution so my question is: Is there a better (more elegant / more correct) way to do this (particularly the processing of the 2 $_POST arrays)? Can I create one associative array with the ID as key and the form input value as the corresponding value?