当前位置: 动力学知识库 > 问答 > 编程问答 >

multi select - grocery crud multiselect field

问题描述:

No problem to use this function 'multiselect field' which show on this website:

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

$crud->field_type('fruits','multiselect',

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

Next step, i try to extract data from database to replace the 'array' in the formulae above but failed, pls advise.

$this->db->select('employeeNumber');

$a = $this->db->get('employees')->result();

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

I'm getting result like

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

[1] => stdClass Object ( [employeeNumber] => 1056 )

Hmm... how to make it into this format, any advise?:

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

网友答案:

You need to actually do a foreach here. In our case you need to do something like this:

$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); 

or it is even better if you have the name of the employer to do something like this:

$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); 
分享给朋友:
您可能感兴趣的文章:
随机阅读: