[arrays] PHP array value passes to next row

I have an array building multiple instances of the following fields:

<div class="checkbox_vm">     <input type="hidden" name="fk_row[]" value="<?php echo $i; ?>">     <input type="hidden" name="fk_id[]" value="<?php echo $vehicle_feature[$i]->fk_id; ?>">     <input type="checkbox" class="checkbox_veh" id="checkbox_addveh<?php echo $i; ?>" <?php if ($vehicle_feature[$i]->check) echo "checked"; ?> name="feature[]" value="<?php echo $vehicle_feature[$i]->id; ?>">     <label for="checkbox_addveh<?php echo $i; ?>"><?php echo $vehicle_feature[$i]->name; ?></label>     <input type="text" class="" id="input_addveh<?php echo $i; ?>" name="featureInput[]" value="<?php echo $vehicle_feature[$i]->fk_value; ?>"> </div> 

The following actions work as intended:

  • Checking a checkbox and enter text in the input field creates a new record
  • Editing the text in an input field (that still has a checkbox checked) saves the changes
  • Unchecking a checkbox removes the record/row from the database

This is the problem:

  • If you UNCHECK a checkbox and save, whatever text/value was in the input field next to it will then be carried over into the next row in the array

Example: the 3rd checkbox had a value of 3, and the 4th checkbox had a value of 4. If you uncheck the 3rd checkbox then save, the 4th checkbox's value is now 3. And every row/record after that is updated to the previous row's value.

Here is the script:

function saveVehicle($option, $task){     global $database;     $fk_row = $_POST['fk_row'];     $fk_id = $_POST['fk_id'];     $feature = $_POST['feature'];     $featureInput = $_POST['featureInput'];     for ($i = 0; $i < count($fk_row); $i++) {         if (isset($feature[$i])){             if ($fk_id[$i]){                 $database->setQuery("UPDATE #__vehiclemanager_feature_vehicles SET fk_vehicleid = $vehicle->id, fk_featureid = $feature[$i], fk_value = '$featureInput[$i]' WHERE id = $fk_id[$i]");                 $database->query();             } else {                 $database->setQuery("INSERT INTO #__vehiclemanager_feature_vehicles (fk_vehicleid, fk_featureid, fk_value) VALUES ($vehicle->id, $feature[$i], '$featureInput[$i]')");                 $database->query();             }         } else {             if ($fk_id[$i]){                 $database->setQuery("DELETE FROM #__vehiclemanager_feature_vehicles WHERE id = $fk_id[$i]");                 $database->query();             }         }     } } 

This question is related to arrays skip

The answer is

Change the checkboxes so that the name includes the index inside the brackets:

<input type="checkbox" class="checkbox_veh" id="checkbox_addveh<?php echo $i; ?>" <?php if ($vehicle_feature[$i]->check) echo "checked"; ?> name="feature[<?php echo $i; ?>]" value="<?php echo $vehicle_feature[$i]->id; ?>"> 

The checkboxes that aren't checked are never submitted. The boxes that are checked get submitted, but they get numbered consecutively from 0, and won't have the same indexes as the other corresponding input fields.