Welcome Guest, Not a member yet? Register   Sign In
Dynamically fill an array from Mysql using Form Helper for dropdown list.
#7

[eluser]DCZ[/eluser]
I might have an alternative for this function
(Remark the flexibility of the query could be better)

In your model or helper

Code:
function get_dropdown_array($key, $value, $from){
        $result = array();
        $array_keys_values = $this->db->query('select '.$key.', '.$value.' from '.$from.' order by id asc');
       foreach ($array_keys_values->result() as $row)
        {
            $result[$row->$key]= $row->$value;
        }
        return $result;
    }

In your controler :
Let's request some countries....

Code:
$this->data['content_countries'] = $this->my_model->get_dropdown_array('id', 'country','countries');


In your view :

Code:
<?=form_dropdown('country_id', $content_countries,$my_country_id);?>

Remark "$my_country_id" is the "selected" value on load.

We can update that last line some more.

Code:
<?=form_dropdown('country_id', $content_countries,set_value('country_id', $my_country_id))?>

So now it will select $my_country_id on first load and then it will remember it value on a failed submit.

Greetz.


Messages In This Thread
Dynamically fill an array from Mysql using Form Helper for dropdown list. - by El Forum - 05-27-2009, 02:24 AM



Theme © iAndrew 2016 - Forum software by © MyBB