Search code examples
multi-selectgrocery-crud

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

Solution

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