问题描述
我正在使用Codeigniter查询数据库并返回数据数组.
I am using Codeigniter to query my database and return an array of data.
我有一个像这样的数据数组:
I have got an array of data like so :
Array
(
[0] => stdClass Object
(
[depot_id] => 1
[depot_name] => Stockton On Tees
[depot_description] => Arriva Stockton on Tees Depot
[depot_postcode] => TS18 3AW
[depot_lat] =>
[depot_long] =>
[operating_company_id] => 1
[date_created] => 2014-02-14 10:24:17
[date_edited] =>
[edited_by] =>
[status] => active
[operating_company_name] => Arriva North East
[operating_company_description] => Arriva North East
[operating_company_lat] =>
[operating_company_long] =>
[operating_company_postcode] =>
[operating_group_id] => 1
)
[1] => stdClass Object
(
[depot_id] => 2
[depot_name] => Darlington
[depot_description] => Arriva Darlington Depot
[depot_postcode] => DH1 1TW
[depot_lat] =>
[depot_long] =>
[operating_company_id] => 1
[date_created] => 2014-02-14 10:24:17
[date_edited] =>
[edited_by] =>
[status] => active
[operating_company_name] => Arriva North East
[operating_company_description] => Arriva North East
[operating_company_lat] =>
[operating_company_long] =>
[operating_company_postcode] =>
[operating_group_id] => 1
)
[2] => stdClass Object
(
[depot_id] => 3
[depot_name] => Ashington
[depot_description] => Arriva Ashington Depot
[depot_postcode] => NE63 9UN
[depot_lat] =>
[depot_long] =>
[operating_company_id] => 2
[date_created] => 2014-02-14 10:46:05
[date_edited] =>
[edited_by] =>
[status] => active
[operating_company_name] => Arriva Northumbria
[operating_company_description] => Arriva Northumbria
[operating_company_lat] =>
[operating_company_long] =>
[operating_company_postcode] =>
[operating_group_id] => 1
)
[3] => stdClass Object
(
[depot_id] => 4
[depot_name] => Blyth
[depot_description] => Arriva Blyth Depot
[depot_postcode] => NE24 2AP
[depot_lat] =>
[depot_long] =>
[operating_company_id] => 2
[date_created] => 2014-02-14 10:46:05
[date_edited] =>
[edited_by] =>
[status] => active
[operating_company_name] => Arriva Northumbria
[operating_company_description] => Arriva Northumbria
[operating_company_lat] =>
[operating_company_long] =>
[operating_company_postcode] =>
[operating_group_id] => 1
)
我想基于运营公司名称"创建一个optgroup,因此在此示例中,有2个仓库位于其下.
I would like to create an optgroup based on the "Operating Company Name" so in this example there are 2 depots that fall underneath it.
在我看来,我目前仅使用一个foreach循环来创建下拉列表.
In my view, I am current just using a foreach loop to create the dropdown.
<select name="depot_id" class="form-control">
<?php foreach($depots as $depot): ?>
<optgroup label="<?php echo $depot->operating_company_name; ?>">
<option value="<?php echo $depot->depot_id; ?>"><?php echo $depot->depot_name; ?></option>
</optgroup>
<?php endforeach; ?>
</select>
这将产生一个下拉列表,如下所示....
That is producing an dropdown as follows....
我如何(如果可能)在循环中将每个操作组和软件仓库放在一起?
How can I (If possible) in the loop put every operating group and depot together?
如果需要,可以提供我的MySQL查询.
Can give my MySQL Query if needed.
谢谢
推荐答案
尝试一下,首先重新格式化源数组,如下所示:
Try, first reformat the source array like below :
$result = array();
foreach($depots as $depot){
$result[$depot->operating_company_name][] = $depot;
}
然后创建选择尝试,
<select name="depot_id" class="form-control">
<?php foreach($result as $key=>$val): ?>
<optgroup label="<?php echo $key; ?>">
<?php foreach($val as $option): ?>
<option value="<?php echo $option->depot_id; ?>"><?php echo $option->depot_name; ?></option>
<?php endforeach; ?>
</optgroup>
<?php endforeach; ?>
</select>
这篇关于根据数据数组创建一个Optgroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!