简体   繁体   English

为动态下拉列表重建数组

[英]Rebuilding array for dynamic dropdowns

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. 我正在尝试创建动态下拉列表,以便第一个下拉列表显示每个不同的location_name,并且根据选择,第二个下拉列表包含相应的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: 如果我选择Office 1,则下一个下拉列表将包含:

<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. 问题是,第一个下拉列表显示每个条目,因此它显示了Office 1,Office 2和Office 5的多个实例。我希望它仅显示每个位置名称中的一个,然后在下一个下拉列表中加载children(display_name)所选位置。 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. 我还需要显示中的ID才能将其保存到数据库中,因此我在考虑解决此问题后应该可以将其包含在表单中。

What am I doing wrong with the array though? 我在用数组做错什么呢?

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. 另外,现在您可以轻松创建其他下拉菜单。

$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); 的print_r($ displayNames);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM