Welcome Guest, Not a member yet? Register   Sign In
populating a form from DB
#1

[eluser]Unknown[/eluser]
Hi,

I just started working with CI, and so far i am really enjoying playing around with it.

There's one thing that i didn't figure out if it can do or not yet is -
Can CI populate forms directly from the DB?
(just like it can populate the DB right from the $_POST i expected to find a similar function to populate the form back)

Thanks,
Roy
#2

[eluser]pistolPete[/eluser]
Have a look at this thread: http://ellislab.com/forums/viewreply/533725/
#3

[eluser]Unknown[/eluser]
i read through the thread and set_value() is just like populating the form my self

i don't see the difference between doing value=<?=$fieldname?> and value=<?=set_value('fieldname')?>
in both ways i need to go through all my fields and manually set their value

and because it's coming from the DB i'll definitely need to convert/manipulate most of the data

i figured since there's a way of giving the db class the whole $_POST array and table name, it'll know how to do the opposite, get the same row back and the form and fill it.

edit :
just for example -
Code:
<select name="Kids">
                    <option value="0" &lt;?= $row->Kids == 0 ? 'selected="selected"' : '' ?&gt;>0</option>
                    <option value="1" &lt;?= $row->Kids == 1 ? 'selected="selected"' : '' ?&gt;>1</option>
                    <option value="2" &lt;?= $row->Kids == 2 ? 'selected="selected"' : '' ?&gt;>2</option>
                    <option value="3" &lt;?= $row->Kids == 3 ? 'selected="selected"' : '' ?&gt;>3</option>
                    <option value="4" &lt;?= $row->Kids == 4 ? 'selected="selected"' : '' ?&gt;>4</option>
                    <option value="5" &lt;?= $row->Kids == 5 ? 'selected="selected"' : '' ?&gt;>5</option>
                    <option value="6" &lt;?= $row->Kids == 6 ? 'selected="selected"' : '' ?&gt;>6</option>
                    <option value="7" &lt;?= $row->Kids == 7 ? 'selected="selected"' : '' ?&gt;>7+</option>
                </select>

That's the only way i found so far to fill in the form's drop downs.

Am i missing something?

Thanks anyway.
Roy
#4

[eluser]Skuja[/eluser]
i suggest you to use form_dropdown() function from form_helper, it will make your code look much cleaner and there will be no problems with repopulation of dropdownmenu.

In your controller:
Code:
$data['kids_options'] = array('
0 => 0,
1 => 1,
2 => 2,
3 => 3
');
$data['selected_kid'] = $row->Kids;
In your view:
Code:
&lt;?=form_dropdown('Kids', $kids_options, $selected_kid)?&gt;
#5

[eluser]Colin Williams[/eluser]
I think set_value() is a fairly useless function, especially when using a form view that is either populated with submitted results or populated with default values. $_POST is already a key => value data structure, and what you return from your model is likely a key => value structure as well. Given that, consider this kind of code in an edit function:

Code:
$form['values'] = get_blog_post_array($id);
if (count($_POST))
{
  $form['values'] = $this->input->xss_clean($_POST);
}

Then in your view, you access values via $values['key']:

Code:
&lt;input type="text" value="&lt;?php print form_prep($values['title']) ?&gt;" name="title" /&gt;
&lt;textarea name="body"&gt;&lt;?php print htmlspecialchars($values['body']) ?&gt;&lt;/textarea&gt;

You can also do some more general sanitizing in your controller if you wish (like form_prep(), etc).




Theme © iAndrew 2016 - Forum software by © MyBB