2014-02-19 275 views
1

我正在使用Codeigniter來查詢我的數據庫並返回數據數組。從數據數組創建一個Optgroup

我有一組數據,像這樣:

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 
    ) 

我想所以在這個例子中有2個倉庫落在它下面創建基於「運營公司名稱」的OPTGROUP。

在我看來,我目前只是使用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> 

這如下產生下拉....

Dropdown Optgroup

我(如果可能)在循環如何可以把每一個工作組和車廠在一起嗎?

如果需要,可以給我的MySQL查詢。

感謝

+0

我認爲你將不得不使用雙的foreach循環來創建你想要的 – Newbi3

+0

試試這個:http://ellislab.com/forums/viewthread/129610/#639772 –

+0

嗨@ Newbi3我是雙重循環雖然? – StuBlackett

回答

4

試試,先重新格式化源陣列象下面這樣:

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

Absolutley精湛!感謝那 – StuBlackett