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")
4

1 回答 1

4

你实际上需要在这里做一个 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); 
于 2013-10-16T22:30:35.140 回答