I'm trying to take a query result, build into an associative array where I use the high level for a general dropdown and that dropdown selection will load cooresponding children of the array.
My current function:
$getDisplays = "
SELECT *
FROM locations l
inner join displays d
ON d.location_id = l.id;";
$displayResult = $mysqlConn->query($getDisplays);
This returns:
Array
(
[ID] => 1
[location_name] => Office 1
[display_name] => lobby
[location_id] => 1
)
Array
(
[ID] => 2
[location_name] => Office 1
[display_name] => break room
[location_id] => 1
)
Array
(
[ID] => 3
[location_name] => Office 2
[display_name] => lobby
[location_id] => 2
)
Array
(
[ID] => 4
[location_name] => Office 2
[display_name] => break room
[location_id] => 2
)
Array
(
[ID] => 10
[location_name] => Office 5
[display_name] => Break Room
[location_id] => 5
)
Array
(
[ID] => 11
[location_name] => Office 5
[display_name] => Lobby
[location_id] => 5
)
Array
(
[ID] => 12
[location_name] => Office 5
[display_name] => Conference Room
[location_id] => 5
)
I'm trying to create dynamic dropdowns so that the first one shows each distinct location_name, and depending on the selection, the second dropdown contains the corresponding display_name.
So for this array, the first dropdown would have:
<option>Office 1</option>
<option>Office 2</option>
<option>Office 5</option>
and if I select Office 1 the next dropdown would contain:
<option>lobby</option>
<option>break room</option>
The way I'm building them now:
<label for="plantSelect">Select A Location</label>
<select class="form-control" id="plantSelect">
<?php foreach($displayResult as $area=>$display):?>
<option><?php echo $display['location_name']?></option>
<?php endforeach;?>
</select>
<label for="areaSelect">Select An Area</label>
<select class="form-control" id="areaSelect">
<option>Please Choose a Location</option>
</select>
The problem is, the first dropdown is showing each entry so it's showing multiple instances of Office 1, Office 2 and Office 5. I want it to only show one of each location name and then load the next dropdown with the children(display_name) of the selected location. I will also need the ID from display in order to save that to a database, so I'm thinking once I fix this issue I should just be able to include that later in a form.
What am I doing wrong with the array though?