Create An Optgroup from an Array of data

非 Y 不嫁゛ 提交于 2019-12-23 03:42:05

问题


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
    )

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.

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?

Can give my MySQL Query if needed.

Thanks


回答1:


Try, first reformat the source array like below :

$result = array();
foreach($depots as $depot){
   $result[$depot->operating_company_name][] = $depot;
}

Then for creating the select try,

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


来源:https://stackoverflow.com/questions/21876224/create-an-optgroup-from-an-array-of-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!