2014-02-19 2 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 
    ) 
내가 그 아래 가을이 개 창고가이 예에서는 있도록 "운영 회사 명"을 기반으로 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