[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.

Questions with arrays tag:

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array? How to update an "array of objects" with Firestore? How to increment a letter N times per iteration and store in an array? Cloning an array in Javascript/Typescript use Lodash to sort array of object by value TypeScript enum to object array How do I check whether an array contains a string in TypeScript? How to use forEach in vueJs? Program to find largest and second largest number in array How to plot an array in python? How to add and remove item from array in components in Vue 2 console.log(result) returns [object Object]. How do I get result.name? How to map an array of objects in React How to define Typescript Map of key value pair. where key is a number and value is an array of objects Removing object from array in Swift 3 How to group an array of objects by key Find object by its property in array of objects with AngularJS way Getting an object array from an Angular service push object into array How to get first and last element in an array in java? Add key value pair to all objects in array How to convert array into comma separated string in javascript Showing ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0) Angular 2 declaring an array of objects How can I loop through enum values for display in radio buttons? How to convert JSON object to an Typescript array? Angular get object from array by Id Add property to an array of objects Declare an array in TypeScript ValueError: all the input arrays must have same number of dimensions How to convert an Object {} to an Array [] of key-value pairs in JavaScript Check if a value is in an array or not with Excel VBA TypeScript add Object to array with push Filter array to have unique values remove first element from array and return the array minus the first element merge two object arrays with Angular 2 and TypeScript? Creating an Array from a Range in VBA "error: assignment to expression with array type error" when I assign a struct field (C) How do I filter an array with TypeScript in Angular 2? How to generate range of numbers from 0 to n in ES2015 only? TypeError: Invalid dimensions for image data when plotting array with imshow()

Questions with skip tag:

PHP array value passes to next row Skip first couple of lines while reading lines in Python file Print a file, skipping the first X lines, in Bash