使用此网站上显示的“multiselect field”功能没问题:
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")
答案 0 :(得分: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);