0

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?

2 Answers 2

1

You can rebuild your array like this:

$displayNames = array();
foreach($displayResult as $subArray) {
  if(!array_key_exists($subArray['location_name'], $displayNames)) {
    $displayNames[$subArray['location_name']] = array();

  }
    $displayNames[$subArray['location_name']][] = $subArray['display_name'];
}

Now, you can do something like:

<?php foreach($displayNames as $displayName):?>
<option><?php echo key($displayName['location_name'])?></option>
<?php endforeach;?>

Things won't repeat. Plus, now you can easily create other dropdowns.

Sign up to request clarification or add additional context in comments.

1 Comment

So, I tried this and it looks like my first dropdown now only has the correct number of entries but they are all 0's
0
$displayNames = array();
foreach($displayResult as $subArray) {
  if(!array_key_exists($subArray['location_name'], $displayNames)) {
      $displayNames[$subArray['location_id']]['location'] = $subArray['location_name'];
      $displayNames[$subArray['location_id']]['display_name'] = array();
  }

  array_push( $displayNames[$subArray['location_id']]['display_name'], array($subArray['ID'] => $subArray['display_name']));
}

print_r($displayNames);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.