EllisLab text mark
Advanced Search
     
Populate drop down list from database
Posted: 02 September 2010 11:27 AM
Joined: 2010-02-09
25 posts

I`m using this code to repopulate drop down list from the database :

$city_id 15;
  while(
$row mysql_fetch_assoc($result)) 
          $selected 
= ($row['city_id'== $city_id) ? 'selected="selected" ' NULL;
          echo 
'<option value="'.$city_id .$selected '">"'.$row['city_name'].'"</option>\n';

  

It works like a charm but which is the best practice in codeigniter ?

 
Posted: 02 September 2010 11:52 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-03
728 posts

I usually do something like this:
Controller:

$data['city_list'$this->City_model->get_dropdown_list();
$this->load->view('my_view_file'$data); 

Model:

function get_dropdown_list()
{
  $this
->db->from('city');
  
$this->db->order_by('name');
  
$result $this->db->get();
  
$return = array();
  if(
$result->num_rows() > 0{
    
foreach($result->result_array() as $row{
      $return[$row[
'id']] $row['name'];
    
}
  }

  
return $return;

View:

<?php echo form_dropdown('city_id'$city_listset_value('city_id'$city_id)); 

Totally untested and especially the get_dropdown_list function can be improved a lot, but I think you get what I’m doing here.

 Signature 

Blog - Twitter

DBlog

MeNeedz: Auth - Cloud - Password - Search - Shoutbox - Akismet -
Twitter - Visitor tracking

 
Posted: 02 September 2010 01:09 PM   [ # 2 ]   [ Rating: 0 ]
Joined: 2010-02-09
25 posts

I`m using almost same code as yours. My question is about how to populate drop down list in edit page. For example a user is registered with selected city lets say New York. When he goes to his edit profile page there is a form and a drop box list with New York selected. This is what i`m trying to handle.

 
Posted: 02 September 2010 02:34 PM   [ # 3 ]   [ Rating: 0 ]
Joined: 2010-02-09
25 posts

I create helper function which is working well

if ( ! function_exists('drop_down'))
{
    
function drop_down($name$match$data)
    
{
        $form 
'<select name="'.$name.'"> ' ."\n";
        
        foreach(
$data as $key => $value)
        
{                                
            $selected 
= ($match == $key) ? 'selected="selected"' NULL ;
            
$form .= '<option value="'$key .'" '$selected .'>'.$value.'' "\n";
        
}           
        
        $form 
.= '</select>' "\n";
        return 
$form;
    
}

In the view

echo drop_down('mylist'3$data); 

Where mylist is the name of select element. 3 is the number which will be matched against $data and $data is associative array containing id and value of option.
Any comments ?

 
Posted: 02 September 2010 02:43 PM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-03
728 posts

Why not use the form_dropdown() function CI provides out of the box?

 Signature 

Blog - Twitter

DBlog

MeNeedz: Auth - Cloud - Password - Search - Shoutbox - Akismet -
Twitter - Visitor tracking

 
Posted: 02 September 2010 02:58 PM   [ # 5 ]   [ Rating: 0 ]
Joined: 2010-02-09
25 posts

Because I should read user guide carefully….
Thanks

 
Posted: 02 September 2010 03:14 PM   [ # 6 ]   [ Rating: 0 ]
Joined: 2009-08-20
243 posts

I often times will have need of multiple fields from a table for one use and two specific fields from the same result set for a select so I wrote the below function.

/**
     * Create an array with key/value pairs from an array of objects/arrays.
     *   
     * @author Daniel Razafsky
     * @param array - Array of objects/arrays to get keys and values from
     * @param object - The element(s) to use as keys. To use multiple values, seperate element names with |
     * @param object - The element(s) to use as values. To use multiple values, seperate element names with |. Passing null will assign the entire element to the returned array subelement.
     * @return array - The newly created array
 */
if(!function_exists('dropdown_array')) {
    
function dropdown_array($array$key$value null$keep_empty_values FALSE{
        
if(!is_array($array)) { $array = array($array); }
        $ret 
= array('' => '&nbsp;');
        
        
$key     explode('|'$key);
        
$value     is_null($value) ? null explode('|'$value);
        
        foreach(
$array as $item{
            $bInclude 
TRUE;
            
$item = (array) $item;
            
            
// Build the key
            
$key_val '';
            foreach(
$key as $k{
                
if(!isset($item[$k])) { $bInclude FALSE}
                
else { $key_val .= $item[$k] ' - '}
            }
            $key_val 
substr($key_val0strlen($key_val) - 3);
            
            
// Build the value
            
$value_val '';
            if(!
is_null($value)) {
                
foreach($value as $v{
                    
if(!isset($item[$v])) { $bInclude FALSE}
                    
else { $value_val .= $item[$v] ' - '}
                }
            }
            $value_val 
substr($value_val0strlen($value_val) - 3);
            
            if(
$bInclude && $keep_empty_values || is_null($value) || !empty($value_val)) {
                $ret[$key_val] 
= !is_null($value) ? $value_val $item;
            
}
        }
        
        
return $ret;
    
}

Using this I can do a select * on my table and still easily build an array for my select without having to write a foreach or a new function every time.

 Signature 

// Because killer robots like unicorns!