Dynamically Dropdown Value On Code Igniter

Yeah, Code Igniter again. I this day i got problem with dynamically dropdown value on Code Igniter. First think i hope I can generate dropdown with values from mySql database. I want to use form helper that provided by Code Igniter to generate form.

Basically to make dropdown input on controller are:

1
2
3
4
5
6
...
$data['input_category'] = array(
	"1" => "Mobile",
        "2" => "Web"
); 
...

Then in views:

1
2
3
...
<?php echo form_dropdown('category',$input_category);?>
...

Prety simple right??
but now.. How to make it values generate dynamically??

I asking my master (”Google”) for this solution. I found this thread from Code Igniter forums, but still not help. Then became hopeless. Haha.. That’s not me actually..
I remember that I created script for split string into array on previous post. Basic on this idea, I’ll created each array to string and then split it into an array. It’s working, but I got “‘1′ => ‘Mobile’” on my dropdown input and array started to 0. So “1″ => “Mobile”’s select name became 0 not id of mobile.
Hmm..
Googling again and I found function array_combine.. Thatha..
Yeah I got that I want and I know what should I do :-)
This is my final script to make dynamically dropdown on Code Igniter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$cat_id="";
$category="";
$query=$this->dn_categories_model->get_all();
foreach($query as $row):
$cat_id .= "$row->cat_id";
$cat_id .= ",";
$category .= "$row->category";
$category .= ",";
endforeach;
$comma=",";//separate by commas
$arr_cat_id=split($comma,$cat_id); 
$arr_category=split($comma,$category); 
$options=array_combine($arr_cat_id,$arr_category);
 
$data['input_category'] = $options;

Fiuh..
I wrote it to on this thread.



Related Post:

One Comment Trackback URL | Comments RSS

  1. gracie Says:

    win, blog mu udah aku link di blogroll ku :D
    link aku jg dong.. http://www.myscrapboxes.com :D
    sukses ya buat kerjaanmu :D

Post a Comment

Your email is never published nor shared. You're allow to say what you want...

Blogroll Link Update