CodeIgniter Forums

Full Version: form_dropdown does not echo selected value from database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

Hello All

I am trying to display in an Edit view results from database. it all works fine for the textboxs, but when i try to display the value at a dropdown box it displays null

here is my code:

$data['states'] = $this->model->get_states();
$data['student_info'] = $this->model->get_student_info($id);

<?php echo form_dropdown('field_name', $states, $student_info['state'] ) ; ?>

i do get the drop down box with the states but the third parameter which suppose to echo the value from the database does not show

thanks in advanced,

El Forum

The third value will select the option in the dropdown which has the matching 'value'. So for this to work your $data['states'] array must be in the form
array('New York' => 'New York', 'Another State' => 'Another State');

Is that the format $this->model->get_states() returns its results as?

El Forum

Take a look at $student_info['state'] and see if it is actually the value you want.

Personally I like to use die($var) to check values, but you've got a whole range of functions like var_dump, print_r, echo...

El Forum

the get_states() function returns the states in the same format the selected value is.


El Forum

So it returns them in a format like array('State', 'State 2'), well if so that's why its not working. Because that actually is an array like array(0 => 'State', 1 => 'State 2'); So when you try to set the value of 'State' it can't find that value in the list of array keys.

El Forum

This should work.

function index()
    //loading the data from the database
    $data['query'] = $this->db->get('states');

    //other stuff here, such as loading views.

<?= form_open('/'); ?>

<select name="state_list">
    &lt;? foreach($query->result() as $row): ?&gt;
        <option value="&lt;?= $row->state_id; ?&gt;" &lt;?= set_select('state_list', $row->state_id); ?&gt;
            &lt;?= $row->state_name; ?&gt;

    &lt;? endforeach; ?&gt;


&lt;?= form_close(); ?&gt;

Anyway, that should give you a basic idea as to how to load from the database. I included the set_select function in there as well.

What this does is queries the results of a table with the name "states". Inside the table, I have the columns, "state_id" and "state_name". You can see how those are used in the View. For the "value" in the option, I use the "state_id", this is a best practices thing and you can replace it with whatever you wish. In a faux scenario, the state_id would be posted to another table, where it holds a relationship with the states table.

I personally had some trouble getting the set_select to work properly but eventually figured it out. As many of you can probably relate, it's one of those things where you SWEAR you have tried it before, then all of a sudden it starts working. Smile

I hope I was able to help you (or someone else)!