【问题标题】:Create An Optgroup from an Array of data从数据数组创建一个 Optgroup
【发布时间】:2014-03-19 12:45:50
【问题描述】:

我正在使用 Codeigniter 查询我的数据库并返回一个数据数组。

我有一个这样的数据数组:

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
    )

我想根据“Operating Company Name”创建一个 optgroup,因此在本例中,它下面有 2 个 depot。

在我看来,我目前只是使用 foreach 循环来创建下拉菜单。

            <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>

这会产生如下下拉列表....

我如何(如果可能)在循环中将每个运营组和仓库放在一起?

如果需要,可以提供我的 MySQL 查询。

谢谢

【问题讨论】:

  • 我认为你必须使用双 foreach 循环来创建你想要的东西
  • 嗨@Newbi3 我在双循环什么?
  • 在解决你的问题之后。您需要循环一次才能将操作名称放入数组中。并使用名称作为键来构建数组。 $optgroupArray[$depot->operating_company_name]
  • 对于每场比赛,添加仓库以制作二维数组。

标签: php mysql arrays codeigniter


【解决方案1】:

尝试,首先重新格式化源数组,如下所示:

$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>

【讨论】:

  • 绝对一流!谢谢你
猜你喜欢
  • 2015-09-11
  • 2019-05-26
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 2012-12-16
  • 2017-05-06
  • 2013-08-21
相关资源
最近更新 更多