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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php if(!defined('BASEPATH')) exit('No direct script access allowed'); function listData($table,$name,$value,$orderBy='ASC') { $items = array(); $CI =& get_instance(); if($orderBy) { $CI->db->order_by($value,$orderBy); } $query = $CI->db->select("$name,$value")->from($table)->get(); if ($query->num_rows() > 0) { foreach($query->result() as $data) { $items[$data->$name] = $data->$value; } $query->free_result(); return $items; } } /* End of file dropdwon_helper.php */ /* Location: ./application/helper/dropdown_helper.php */ |
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).
1 |
$this->load->helper('dropdown'); |
How to Use Show Me Example
Create a controller class called test.php in application/controllers/test.php, then copy past below script
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php class Test extends CI_Controller { public function index() { $this->load->helper(array('dropdown','form')); $dropdownItems = listData('country_tbl','country_id', 'country_name'); echo form_dropdown('dropdown',$dropdownItems,$selected = 8); } } /* End of file test.php */ /* Location: ./application/controllers/test.php */ |
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.
I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.
Great, I am gonna use this helper !
Hi sir! How can i show dropdownlist to my form?
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);
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.
i didn’t get you
Something likes this. Select cUnit FROM ITEM_UNIT WHERE cItemCode = ‘Item Code Field’.
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
Hi Arjun,
How do I extract the value of the selected item in the dropdown list?
Thanks,
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 ];
Hi Arjun,
You’re Awesome! Thank you very much, may you have a Prosperous New Year!
how to enable gzip in codeigniter
Here is the post http://arjunphp.com/codeigniter-gizip/
hi arjun
can i create two dependent dropdown by using codeigniter helper
if yes then how
thanks
waiting for reply
Hi,arjun,can you help me to do nested submenu in a dropdown list using codeigniter?
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..
$(document).ready(function(){
$(‘#dropdown’).change(function(){
alert($(this).val());
});
});
use $(this).text() to get option text
read this – https://arjunphp.com/selected-text-value-dropdown-jquery/
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 “
I need to cascade 4 drop down lists and i want to populate datas for a filtering purpose. Can you help me..???
hi arjun i want fetch data in dropdown list from text
sir i m not getting can u mail me a whole code
What is your codeigniter version?
codeigniter 3.1.5 sir
Okay.let me check..
k sir
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
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
Hi Sorry for the delay. Here is the tutorial – https://arjunphp.com/cascading-dropdown-list-codeigniter/
hii sir can u tell me how to fetch multiselected data from cascdaing dropdown in form of array in codeigniter
Instead of pain name attitude use name as array. Example form_dropdown(‘dropdown[],$dropdownItems,$selected = 8);
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
Can you share you JavaScript code?
Really thanks for your respond.Finally i found my bug.The name was different i gave to the ajax select box.