Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 0 down vote accepted
$met = $_POST['meters'];
$id = $_POST['ids'];



if (isset($met) && isset($id)) 
{
    for ($i = 0; $i < count($id); $i++) 
    {
        updateRecord($id[$i],$met[$i]);
    }
}
share|improve this answer
    
Thank you ... although this is almost identical to what I have .. Is there not a way I can do something like $_POST['id']['meter'] where id1 => meter_reading for each id..? –  AcidHawk 2 days ago
    
yes thats true, but truley what you are doing is a optimal solution w.r.t split time –  Smruti Singh 2 days ago

A better way is to make sure you have 'set in stone' names for your inputs;

foreach ($meterdetail as $key => $v2) {
    echo "
<tr>
    <td class='tableText'>
        <input type='hidden' name='id_".$key."' value='".$v2['id']."'>
        <input type='text' name='meter_".$key."' value=''>
    </td>
</tr>
    ";
}

And than process the post-data as such:

$ids = array();
$meters = array();
foreach($_POST as $name => $value) {
    if(strpos($name, 'id_') === 0) {
        $ids[substr($name, 3)] = $value;
    }
    if(strpos($name, 'meter_') === 0) {
        $ids[substr($name, 3)] = $value;
    }
}

if(count($ids) && count($meters)) {
    foreach($ids as $key => $id) {
        updateRecord($id, $meters[$key]);
    }
}

This way you are always sure you have accompanying values which you are updating in the db.

PS: this is proof of concept... haven't tested it yet...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.