Welcome Guest, Not a member yet? Register   Sign In
How To Create CodeIgniter form_dropdown
#1

[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
#2

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

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

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

[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");
#5

[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.
#6

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

[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
#8

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

[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???
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB