CodeIgniter Forums
How To Create CodeIgniter form_dropdown - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: How To Create CodeIgniter form_dropdown (/showthread.php?tid=35235)

Pages: 1 2


How To Create CodeIgniter form_dropdown - El Forum - 10-23-2010

[eluser]Ahmed Iqbal[/eluser]
Hello Friends,

I'm using Dropdown old code for categorie's list.
Look like
Code:
<select name="category" id="category">
&lt;?php foreach ($categories as $cat): ?&gt;
<option label="&lt;?=$cat->cat_name; ?&gt;" value="&lt;?=$cat->cat_id; ?&gt;">
&lt;?=$cat->cat_name; ?&gt;
</option>
&lt;?php endforeach; ?&gt;
</select>

But i want to be use codeigniter 'form_dropdown function'.
By the way, I've already selected form helper in me config/autoload.php.
But i need dropdown code structure for codeingiter...!

Thanks Smile


How To Create CodeIgniter form_dropdown - El Forum - 10-23-2010

[eluser]pickupman[/eluser]
Try
Code:
echo form_dropdown('category', $categories, FALSE, 'id="category");

Note: The label attribute is only supported by IE 7+.


How To Create CodeIgniter form_dropdown - El Forum - 10-23-2010

[eluser]Ahmed Iqbal[/eluser]
Thanks, but how i can get foreach '$cat' result in this 'form_dropdown' function?


How To Create CodeIgniter form_dropdown - El Forum - 10-23-2010

[eluser]pickupman[/eluser]
If you would like to use the form dropdown helper, you need to pass a associative array rather than object. You something in your controller like:
Code:
$data['cat'] = array();
foreach($categories as $row){
$cat[$row->cat_id] = $row->cat_name;
}

$this->load->view('your_view', $data);

//Now in your view
echo form_dropdown('category', $cat, set_value('category'), 'id="category");



How To Create CodeIgniter form_dropdown - El Forum - 10-23-2010

[eluser]Ahmed Iqbal[/eluser]
if i dot this
Code:
&lt;?php foreach ($categories as $cat): ?&gt;
&lt;?=form_dropdown('category', $cat, set_value('category'), 'id="category");
&lt;?php endforeach; ?&gt;

it's generate <select> multi time with <option>. so how i fix Sad
I'm confused with this.


How To Create CodeIgniter form_dropdown - El Forum - 10-23-2010

[eluser]pickupman[/eluser]
Thats not the code I posted. Double check my syntax on the foreach loop.


How To Create CodeIgniter form_dropdown - El Forum - 10-23-2010

[eluser]Ahmed Iqbal[/eluser]
Controller Code:
Quote:$data['cat'] = $this->database->get_cat(); // Try To Catch Categories Result From Model
foreach($categories as $row){
$cat[$row->cat_id] = $row->cat_name;
}
$this->load->view('share_view', $data);

View Code:
Quote:&lt;?=form_dropdown('category', $cat, set_value('category'), 'id="category"'); ?&gt;

Error in Result:
Code:
A PHP Error was encountered
Severity: 4096
Message: Object of class stdClass could not be converted to string
Filename: helpers/form_helper.php
Line Number: 331



How To Create CodeIgniter form_dropdown - El Forum - 10-23-2010

[eluser]pickupman[/eluser]
You need to return the result object back to $categories from your model.


How To Create CodeIgniter form_dropdown - El Forum - 10-23-2010

[eluser]Ahmed Iqbal[/eluser]
ohh, sorry...! I've understand your solution.
your code work for me. Smile

thanks man.

Fried, please let me know right solution to access custom build function in controller.
we need to 'require_once' custom functions file in controller?
or maybe we access my custom function through model?

like '$this->load->model('database');'
and '$this->database->custom_function(argument) ??? this is right solution???


How To Create CodeIgniter form_dropdown - El Forum - 10-23-2010

[eluser]pickupman[/eluser]
Yes that will be fine. Remember codeigniter is still php so you can still do the same stuff. If are going to include a custom class check out the user guide for custom libraries. That you can use CI syntax. If it is a set of function you can put into your own helper. Whatever you feel comfortable with is fine. That's one of the things people like about CI is that it doesn't force you into anything.