CodeIgniter Helper :: Quick Dynamic DropDown Select Box ?

/ By Arjun / Published on September 24, 2014

This post is about Quick and Easy Drop-down HTML Select box element generation with custom CI helper function(CI helper file merely contains your defined functions)

You might like this post –
How to create custom helper in CodeIgniter

Here is the steps to create Helper , just crate a file called dropdown_helper.php in application/helper/dropdown_helper.php. then copy paste the following code.

How to use Dropdown helper?

To use this helper, you should load this helper just like ci core helpers,you can load this helper controller level or method level or you can set auto load in applications/config/autoload.php(you can use any were in the application).

How to Use Show Me Example

Create a controller class called test.php in application/controllers/test.php, then copy past below script

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

0 0 votes
Article Rating
Subscribe
Notify of
guest
33 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Kiran
Kiran
7 years ago

Great, I am gonna use this helper !

Kanor
Kanor
6 years ago

Hi sir! How can i show dropdownlist to my form?

arjun
6 years ago
Reply to  Kanor

Just pass the array to views , then use ci’s form_dropdown function. below is the example

Ex:
public function index() {
$this->load->helper(array(‘dropdown’,’form’));
$data[‘dropdownItems’] = listData(‘country_tbl’,’country_id’, ‘country_name’);
$this->load->view(‘your_view’,$data);
}

now in your view file :
echo form_dropdown(‘dropdown’,$dropdownItems,$selected = 8);

Shortcut:
echo form_dropdown(‘dropdown’,listData(‘country_tbl’,’country_id’, ‘country_name’),$selected = 8);

Kanor
Kanor
6 years ago
Reply to  arjun

It is displaying data now. But how can i set a condition, I have a Table Named: ITEM_UNIT. Columns: cItemNo | cUnit | nFactor. Whenever i pick an item it will get only the units refer to the Item Code.

arjun
6 years ago
Reply to  Kanor

i didn’t get you

Kanor
Kanor
6 years ago
Reply to  arjun

Something likes this. Select cUnit FROM ITEM_UNIT WHERE cItemCode = ‘Item Code Field’.

arjun
6 years ago
Reply to  Kanor

Select cUnit, cUnitText FROM ITEM_UNIT WHERE cItemCode = ‘Item Code Field’.

echo form_dropdown(‘dropdown’,listData(‘country_tbl’,’country_id’, ‘country_name’),$selected = 8);

replace : country_tbl with your table

replace : country_id with your field name – which will set as value

replace : country_name with field name – which will appear as text

output:
country_name

manny
manny
6 years ago

Hi Arjun,

How do I extract the value of the selected item in the dropdown list?

Thanks,

arjun
6 years ago
Reply to  manny

use – listData function and it will return array(), from the you can easily extract selected values , some thing like :
$dropdownItems = listData(‘country_tbl’,’country_id’, ‘country_name’);
$country_id = $this->input->post(‘country_id’);
echo $dropdownItem[$country_id ];

manny
manny
6 years ago
Reply to  arjun

Hi Arjun,

You’re Awesome! Thank you very much, may you have a Prosperous New Year!

Rakesh Vishwakarma
Rakesh Vishwakarma
6 years ago

how to enable gzip in codeigniter

arjun
6 years ago
suraj bhan pandey
suraj bhan pandey
5 years ago

hi arjun
can i create two dependent dropdown by using codeigniter helper
if yes then how
thanks
waiting for reply

Shany Mathew
Shany Mathew
5 years ago

Hi,arjun,can you help me to do nested submenu in a dropdown list using codeigniter?

Venkat Prakash
Venkat Prakash
5 years ago

hi arjun how to get values by ON SELECT method..i.e., if I select any option it will give values of that selected option on the same screen.what can I write in javascript?/

can u plz help me..
thanks.
waiting for ur reply..

arjun
5 years ago
Reply to  Venkat Prakash

$(document).ready(function(){
$(‘#dropdown’).change(function(){
alert($(this).val());
});
});

use $(this).text() to get option text

Harshita
Harshita
5 years ago

How do i get the selected values from dropdown select , while updating (codeigniter)
**view**

Question
input->post(‘ques_id’)) echo $this->input->post(‘ques_id’); else echo $data->ques_id; ?>
ques_id);echo “

"; print_R($a);"";?>

$question){ ?>
<option value="" input->post('question') && $this->input->post('question') == $ques_id) || $data->question == $ques_id) echo 'selected';?>>          question?>
value);?>


      

 $(function() {
            $('#question').change(function() {
                console.log($(this).val());
            }).multipleSelect({
                width: '100%'
            });



**controller**

public function edit($interview_id) {
        $this->data['categories']=$this->Interview_model->getcategories();
        $where = ' interview_id = ? ';
        $where_data [] = $interview_id;
        if ($this->input->post ()) {
            $update_data = array ();
            $update_data ['name'] = $this->input->post ( 'name' );
            $update_data ['education'] = $this->input->post ( 'education' );
            $update_data ['ques_id'] = json_encode($this->input->post ( 'question' )); 
            $update_data ['quotes'] = $this->input->post ( 'quotes' );
            $update_data ['comment'] = $this->input->post ( 'comment' );
            $update_data ['status'] = ( int ) $this->input->post ( 'status' );
            $update_data ['Datetime'] = $this->Interview_model->getCurrentDatetime ();
            if (! $this->input->post ( 'old_image' )) {
                $field_name = 'image';
                $temp_file_name = $this->file [$field_name] ['name'];
                $file_name = time () . '_' . $this->randomString ( 10 ) . '.' . $this->getFileExtension ( $temp_file_name );

                if (! $this->uploadImage ( $this->upload_path, $file_name, $field_name )) {
                    $this->session->set_flashdata ( 'error', $this->file_error );
                }
                $this->createThumbnail ( $this->file_data ['full_path'], $this->upload_path . '/thumb/' . $file_name, 75, 50 );
                $update_data ['image'] = $file_name;
            }
            if (! $this->Interview_model->__update ( 'ph_interview', $update_data, array (
                    'interview_id' => $interview_id
            ) )) {
                $this->session->set_flashdata ( 'error', 'Record couldn'n updated. Please try again.' );
            } else {
                $this->session->set_flashdata ( 'success', 'Interview updated successfully.' );
                //redirect ( 'admin/Interview' );
            }
        }
        $rs = $this->Interview_model->__select ( 'ph_interview', '*', $where, $where_data );
        $result = $rs->result ();
        $this->data ['data'] = $result [0];
        $this->load->view ( 'admin/template/header' );
        $this->load->view ( 'admin/Interview/edit', $this->data );
        $this->load->view ( 'admin/template/footer' );
    }

** and model **

$value){
                if(!empty($con)){
                    $con .=' and ';
                }
                $con .=' '.$key.'  ? ';
            }
            $con = trim($con,',');
            $sql .=' where '.$con.' order by question ';
            $rs = $this->db->query($sql,array($where));
        }
        else{
            $sql .=' order by question ';
            $rs = $this->db->query($sql);
        }
        foreach($rs->result() as $result){
            $question[$result->ans] = $result;
        }
        return $question;
    }

    public function getcategories(){
        $sql ="select * from ph_interview_ques order by ques_id asc";
        $rs = $this->db->query($sql);
        foreach($rs->result() as $record ){
            $result[] = $record;
        }
        return $result;
    }

    public function getQuestions($ids){
        $sql ="select * from ph_interview_ques where ques_id in($ids) order by ques_id asc";
        $rs = $this->db->query($sql);
        foreach($rs->result() as $record ){
            $result[] = $record;
        }
        return $result;
    }
    public function getinterviewr($where=array()){
        $category = array();
        $sql ='select * from ph_interview ';
        if(!empty($where)){
            $con='';
            foreach($where as $key=>$value){
                if(!empty($con)){
                    $con .=' and ';
                }
                $con .=' '.$key.'  ? ';
            }
            $con = trim($con,',');
            $sql .=' where '.$con.' order by name ';
            $rs = $this->db->query($sql,array($where));
        }
        else{
            $sql .=' order by name ';
            $rs = $this->db->query($sql);
        }
        foreach($rs->result() as $result){
            $name[$result->name] = $result;
        }
        return $name;
    }

}
?>

if any one know the answer , please comment......thanks in advance.
Pavithramanikandan
Pavithramanikandan
5 years ago

I need to cascade 4 drop down lists and i want to populate datas for a filtering purpose. Can you help me..???

Azam Khan
Azam Khan
4 years ago

hi arjun i want fetch data in dropdown list from text

Gowri Deshpande
Gowri Deshpande
4 years ago

sir i m not getting can u mail me a whole code

arjun
4 years ago

What is your codeigniter version?

Gowri Deshpande
Gowri Deshpande
4 years ago
Reply to  arjun

codeigniter 3.1.5 sir

arjun
4 years ago

Okay.let me check..

Gowri Deshpande
Gowri Deshpande
4 years ago
Reply to  arjun

k sir

chaitra joshi
chaitra joshi
4 years ago
Reply to  arjun

sir..i have tried your code..it is displaying only one dropdown..but i want cascading drop down like for example…if i select country it should display all the states related to that country in another dropdrown..in the same way,if i select ststate it should display cities related to that state in another dropdown using helper functions in codeigniter

Gowri Deshpande
Gowri Deshpande
4 years ago

sir actually i want cascading dropdown code using helper functions in codeigniter..
how can i achive this?? can u plz share a zip file of your code plz sir

arjun
4 years ago

Hi Sorry for the delay. Here is the tutorial – https://arjunphp.com/cascading-dropdown-list-codeigniter/

Raksha Kulkarni
Raksha Kulkarni
4 years ago

hii sir can u tell me how to fetch multiselected data from cascdaing dropdown in form of array in codeigniter

arjun
4 years ago

Instead of pain name attitude use name as array. Example form_dropdown(‘dropdown[],$dropdownItems,$selected = 8);

Sumiah mohd basheer
Sumiah mohd basheer
2 years ago

I have been with this bug for 2 days please help.I have an add row button .when clicked new select box is displayed with some values from database.THis is done using ajax.I am able to add rows.But when it is submited no values are posted.I am doing this in codeigniter.Please help asap

arjun
2 years ago

Can you share you JavaScript code?

Sumiah mohd basheer
Sumiah mohd basheer
2 years ago

Really thanks for your respond.Finally i found my bug.The name was different i gave to the ajax select box.

33
0
Would love your thoughts, please comment.x
()
x