2013-10-16 17 views
2

否使用該功能「多選字段」,這表明,該網站上的問題:雜貨污物多選字段

http://www.grocerycrud.com/documentation/options_functions/field_type 

$crud->field_type('fruits','multiselect', 
        array("1" => "banana", "2" => "orange", "3" => "apple")); 

下一步,我試圖從數據庫中提取數據,以取代在上面的公式中的「數組」但失敗了,請指教。

$this->db->select('employeeNumber'); 
$a = $this->db->get('employees')->result(); 
$crud->field_type('firstName', 'multiselect', $a); 

我越來越像結果

Array ([0] => stdClass Object ([employeeNumber] => 1002) 
     [1] => stdClass Object ([employeeNumber] => 1056) 

嗯......如何將它做成這種格式,任何意見?:

array("1" => "banana", "2" => "orange", "3" => "apple") 

回答

3

你需要真正做一個foreach這裏。在我們的例子,你需要做這樣的事情:

$this->db->select('employeeNumber'); 
$results = $this->db->get('employees')->result(); 
$employees_multiselect = array(); 

foreach ($results as $result) { 
    $employees_multiselect[$result->employeeNumber] = $result->employeeNumber; 
} 

$crud->field_type('firstName', 'multiselect', $employees_multiselect); 

,或者如果你有僱主的名字做這樣的事那就更好了:

$this->db->select('employeeNumber, employeeName'); 
$results = $this->db->get('employees')->result(); 
$employees_multiselect = array(); 

foreach ($results as $result) { 
    $employees_multiselect[$result->employeeNumber] = $result->employeeName; 
} 

$crud->field_type('firstName', 'multiselect', $employees_multiselect); 
+1

非常感謝你:)。還有一件事,如果我想添加一個像這樣的字段:select('employeeNumber,employeeName,familyName');如何輸出? – user1884324

+1

我明白了:$ employees_multiselect [$ result-> employeeNumber] = $ result-> employeeName。 $ result-> familyName; – user1884324

+1

是的,這是正確的做法。這裏唯一缺少的東西是空白。所以你的情況是:$ employees_multiselect [$ result-> employeeNumber] = $ result-> employeeName。「」。 $ result-> familyName;如果這對你有幫助,那麼請添加一個聲譽到答案:-)(這是向上按鈕) –