Welcome Guest, Not a member yet? Register   Sign In
form_dropdown does not echo selected value from database
#6

[eluser]dubstep[/eluser]
This should work.

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

    //other stuff here, such as loading views.
}

View:
Code:
<?= 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;
        </option>

    &lt;? endforeach; ?&gt;

</select>

&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)!



-Ron


Messages In This Thread
form_dropdown does not echo selected value from database - by El Forum - 05-08-2010, 12:27 PM



Theme © iAndrew 2016 - Forum software by © MyBB