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

[eluser]nir[/eluser]
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:

controller:
Code:
$data['states'] = $this->model->get_states();
$data['student_info'] = $this->model->get_student_info($id);
$this->load->view('form.php',$data);

view:
Code:
<?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,
nir
#2

[eluser]adamp1[/eluser]
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
Code:
array('New York' => 'New York', 'Another State' => 'Another State');

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

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

[eluser]nir[/eluser]
the get_states() function returns the states in the same format the selected value is.

thanks,
nir
#5

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




Theme © iAndrew 2016 - Forum software by © MyBB