• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to populate a dropdown from result_array

#1
I have form that needs data from the database. The form is for adding an office. An office belongs to a department, so when a new office is added it should be added to the right department. What I want to do is when I call the add_form view the dropdown in this view should be populated by data which is obtained from the database. Here is my code:
Here is the code that searches through the departments table and retrieve all the departments from the contacts_model
Code:
public function get_departments() {
       //query
       $query = $this->db->get('departments');
       //return the result
       return $query->result_array();
   }
Here is the code that loads the add form view in the phones model
Code:
 //if the passed value is one show the add department view
       if($param == 2){
            $data['title'] = 'Add College Office';
            $departments['departs'] = $this->contacts_model->get_departments();
           
            $this->load->view('templates/header', $data);
            $this->load->view('addoffice.php', $departments);
            $this->load->view('templates/footer');
       }
Here is the code to display my view
Code:
<h1 align="center">ADD COLLEGE OFFICE</h1>
       <div align="center">
 <p><?php echo validation_errors(); ?></p>
 <p><?php echo form_open('phones/addOffice') ?></p>
 <table width="303" border="1">
   <tr>
     <th width="119" scope="row"><?php echo form_label('Office', 'office'); ?></th>
     <td width="168">  
      <?php
       $data = array(
                 'name'        => 'office',
             'id'          => '',
             'value'       => '',
             'maxlength'   => '150',
             'size'        => '100',
             'style'       => 'width:50%');
              echo form_input($data); ?>
             </td>
   </tr>
   
     <tr>
       <th scope="row"><?php echo form_label('Department', 'department'); ?></th>
       <th scope="row"><?php
                            $options = array();
                            foreach($departs as $option){
                                $options = array('option' => $option['names'],
                                                 'value' => $option['id']);
                                
                            }
                            //$options = $tmp;
                            echo form_dropdown('departments', $options, ''); ?></th>
   <tr>
     <th colspan="2" scope="row"><?php echo form_submit('submit', 'Add College Office') ?></th>
   </tr>
 </table>
 
<?php echo form_close(); ?>
</div>

When I run the code the dropbox only shows the first Department and its id from the database
Reply

#2
what you get from the model is an array with departments, the array being an indexed array, but when you pass the data to the dropdown you simply pass it an array which doesn't exist: $departments['names'].
Reply

#3
The most convenient way is by using the PHP function array_column() http://php.net/manual/en/function.array-column.php

In CodeIgniter 3 its presence is guaranteed for PHP <5.5 with a fallback implementation, have a look at this file https://github.com/bcit-ci/CodeIgniter/b...andard.php
Reply

#4
(01-13-2015, 03:23 AM)Avenirer Wrote: what you get from the model is an array with departments, the array being an indexed array, but when you pass the data to the dropdown you simply pass it an array which doesn't exist: $departments['names'].
I updated my question. I later realised I was passing the result_array to the wrong view. Now I have a new error. The dropbox is only showing the first Department name and its id. Iwant it to show all the departments only with the id hidden
Reply

#5
You have to build a proper array for the dropdown function:

PHP Code:
$options = array();
foreach(
$departs as $option)
{
    
$options[$option['id']] = $option['names'];
}
echo 
form_dropdown('shirts'$options); 

Reply

#6
Code:
echo form_dropdown(
            'department',
            array('' => '-- Choose --') + array_column($departments, 'names', 'id'),
            '', // or could be for example set_value('department', $department)
            'id="department"'
        );
Reply

#7
It would be better within Contacts_model a special method public function dropdown() { ... } to be created. In this special method it is not necessary all the fields to be retrieved, 'id' and 'names' are enough.

Code:
public function dropdown() {

    $result = $this->db
        ->select('id', 'names')
        ->get('departments')
        ->result_array();

    return array_columns($result, 'names', 'id');
}


And then the dropdown in the view will become:

Code:
echo form_dropdown(
            'department',
            array('' => '-- Choose --') + $departments /* as a result of $this->contacts_model->dropdown() */,
            '', // or set_value('department', $department)
            'id="department"'
        );
Reply

#8
(01-13-2015, 02:24 PM)Rufnex Wrote: You have to build a proper array for the dropdown function:


PHP Code:
$options = array();
foreach(
$departs as $option)
{
 
$options[$option['id']] = $option['names'];
}
echo 
form_dropdown('shirts'$options); 

thanx i tried your solution and it worked
Reply

#9
There are many times you will want to use that code to put an existing array into the format that the form_dropdown() needs, so it would be best to build a helper instead of repeating the same code all over.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.