//flatten all vaccinations into an associated array //where the keys are the indexes //this makes searching 100% easier $flat_all_vaccinations = array(); foreach ($all_vaccinations as $value) { $flat_all_vaccinations[$value->vaccination_wk] = $value->vaccination_name; } //loop through all POST fields foreach ($_POST as $key => $value) { //exclude the submit and new_vaccination fields if (!in_array($key, array('submit', 'new_vaccination'))) { //at this point, we are checking each field //so now we essentially need to update all fields //but only do so if there is a difference between the database and the form value if ($flat_all_vaccinations[$key] != $value) { $row_to_update = Vaccination::find_by_id($key); $row_to_update->vaccination_name = $value; //try to save if ($row_to_update->save()) { //if the item was changed successfully, add to array $changes[] = "<strong>" . $value . "</strong> was updated successfully."; } else { //if the item was changed successfully, add to array $changes[] = "<strong>" . $value . "</strong> was not updated successfully."; $changes[] = $database->last_error; } } } } //now we're done with all updates //check to see if we need to create a new vaccination record