I have an availability tracker that has many rows representing users with select/option boxes in each row (7 to be exact). This gives me a possibility of 7*num_of_users select boxes.
My UI looks like this:
Monday Tuesday Wednesday Thursday
------------------------------------------
|Name 'status' 'status' 'status' 'status'|
------------------------------------------
|Name 'status' 'status' 'status' 'status'|
------------------------------------------
|Name 'status' 'status' 'status' 'status'|
------------------------------------------
Where each Name is a label and 'status' is an select option box.
<table>
<tr>
<th>
</th>
<th>
Monday
<br/>
Jul 03, 2017
</th>
<th>
Tuesday
<br/>
Jul 04, 2017
</th>
</tr>
<tr>
<td>
Name
</td>
<td>
<select name=status[] style = "color: ; background-color: ""onChange="setColor(this)" >
<option disabled selected value> -- select an option -- </option>
<option value='Available' style = "color: #000000; background-color: #b6d7a8">Available</option>
<option value='Unavailable' style = "color: #ffffff; background-color: #000000">Unavailable</option>
</select>
</td>
<td>
<select name=status[] style = "color: ; background-color: " onChange="setColor(this)" >
<option value='Available' style = "color: #000000; background-color: #b6d7a8">Available</option>
<option value='Unavailable' style = "color: #ffffff; background-color: #000000">Unavailable</option>
</select>
</td>
</tr>
</table>
The issue I'm having is when the user updates the form via button press, if the user does not select an option for every select box then the values do not exist in $_POST['status'][$index] where I'm using index to loop through the 7*num_of_users select boxes.
If only 4 options are selected then the array has 4 elements. How do I go about getting these NULL/empty values?
if(isset($_POST['update']))
{
$query = "SELECT id "
. "FROM pilots "
. "ORDER BY last_name, first_name, id";
$result = mysqli_query($db, $query) or die ('Error querying database');
$index = 0;
while ($row = mysqli_fetch_array($result))
{
for ($day = 0; $day < 7; $day++)
{
$status = $_POST['status'][$index];
$query = "INSERT INTO availability (pilot_id, date, status)
VALUES ('" . $row['id'] . "', '2017-06-26', '" . $status . "')
ON DUPLICATE KEY
UPDATE status='". $status . "'";
mysqli_query($db, $query) or die ('Error querying database');
$index++;
}
}
}