2

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")
user1884324
  • 693
  • 5
  • 14
  • 21

1 Answers1

4

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); 
John Skoumbourdis
  • 3,041
  • 28
  • 34
  • 1
    thank you so much :). One more thing, if i want to add 1 more field like this: select('employeeNumber, employeeName, familyName'); how to output? – user1884324 Oct 17 '13 at 00:59
  • 1
    i got it: $employees_multiselect[$result->employeeNumber] = $result->employeeName . $result->familyName; – user1884324 Oct 17 '13 at 01:22
  • 1
    Yes this is the correct way to do it. The only missing thing here is the whitespace. So in your case it will be: $employees_multiselect[$result->employeeNumber] = $result->employeeName ." ". $result->familyName; If that helped you then please add one reputation to the answer :-) (this is the up button) – John Skoumbourdis Oct 17 '13 at 12:58
  • How do we save the results of multiselect fields? I just get the last id selected in the database. – beNerd Feb 19 '14 at 09:39
  • You have to do it with callbacks. In your case you have to use callback_insert and callback_update – John Skoumbourdis Feb 20 '14 at 11:09