Welcome Guest, Not a member yet? Register   Sign In
Populate drop down list from database
#1

[eluser]adaxa[/eluser]
I`m using this code to repopulate drop down list from the database :

Code:
$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 ?
#2

[eluser]davidbehler[/eluser]
I usually do something like this:
Controller:
Code:
$data['city_list'] = $this->City_model->get_dropdown_list();
$this->load->view('my_view_file', $data);

Model:
Code:
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:
Code:
&lt;?php echo form_dropdown('city_id', $city_list, set_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.
#3

[eluser]adaxa[/eluser]
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.
#4

[eluser]adaxa[/eluser]
I create helper function which is working well
Code:
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
Code:
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 ?
#5

[eluser]davidbehler[/eluser]
Why not use the form_dropdown() function CI provides out of the box?
#6

[eluser]adaxa[/eluser]
Because I should read user guide carefully....
Thanks
#7

[eluser]Kamarg[/eluser]
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.
Code:
/**
     * 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_val, 0, strlen($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_val, 0, strlen($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.




Theme © iAndrew 2016 - Forum software by © MyBB