How to make a select box or form drop down menu using codeiginter.
We already know that the codeignter is best PHP framework ,library and a simple cms. a lot of people agree that codeigniter is library. we discussed some features of codeiginter. Now we are going to learn how to make a select box aka form drop down menu using codeigniter. There is so many ways to make this select box using codeigniter php framework. Good news is that codeigniter released version 2 with lot of features also,unfortunately it supports PHP 5 only.
We can make the select box with codeigniter framework in three ways:
- Directly inputing the associative array to form_dropdown codeigniters HTML helper function.
- Directly inputing an indexed array to form_dropdown codeigniters HTML helper function.
- HTML PHP parsing mechanism,with template without form_dropdown function.
I think all of us know about the type of arrays in php , nevermind letus know a bit cause some of us no alteast one of us dont know.
According to the elements types with array we categorised associate array, indexed array, mixed array.
$options1=array(1,2,3,4,5); //assoc array $options2=array('op1'=>'opton1','op2'=>'opton2','op3'=>'opton3'); //mixed array contains indexed and assoc array elements $options3=array('op1'=>'opton1','op2'=>'opton2',1,4); //associative array $options4=array('op1'=>'opton1','op2'=>'opton2','op3'=>'opton3'); //this rang function will return 3,4,5 elements indexed array $options=range(3,5); |
A mixed array can contain not only different keys ,can also have different type of elements also
form_dropdown has basically four input parameters.
- param1: name the select box or drop down box, it is string.
- param2: any indexed or associative array
- param3: default selected options in select box with array format
- param4: this is the string which can be printed on selecte box attributes directly.
Even though form_dropdown has four parameters, we must pass alteast two parameters param1 and param2.
Before going to start our example we must know codeignite MVC mechanism, atleast about controller. no problem we see all examples in single controller called select.php and i provided inline comments that will help us .
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Select extends CI_Controller { function __construct() { parent::__construct(); } function index() { $this->load->helper('form'); //indexed array $options1=array(1,2,3,4,5); //assoc array $options2=array('op1'=>'opton1','op2'=>'opton2','op3'=>'opton3'); //mixed array contains indexed and assoc array elements $options3=array('op1'=>'opton1','op2'=>'opton2',1,4); //assoc array $options4=array('op1'=>'opton1','op2'=>'opton2','op3'=>'opton3'); //add one more element $options4['op4']='option4'; //creates dynamic indexed array from 3 to 5 values $options=range(3,5); //array elements which are selected by default , these are keys of the options array $selected_opt=array('op1','op3'); $selected_opt2=array(1,2); //adding extra HTML attributes to form drop down with a string $extra_attributes1='id="dropy" style="color:blue;"'; echo form_dropdown('drop1', $options); echo form_dropdown('drop2', $options1); echo '<br/><br/>'; echo form_dropdown('drop3', $options2); echo '<br/><br/>'; echo form_dropdown('drop4', $options3); echo '<br/><br/>'; echo form_dropdown('drop5', $options4); echo '<br/><br/>'; echo form_dropdown('drop6', $options2,'op3'); echo '<br/><br/>'; echo form_dropdown('drop7', $options2,$selected_opt); echo '<br/><br/>'; echo form_dropdown('drop8', $options2,$selected_opt,$extra_attributes1); echo '<br/><br/>'; echo form_dropdown('drop9', $options2,$selected_opt,'id="dropu" onclick="alert('you changed options!!');"'); echo '<br/><br/>'; echo form_dropdown('drop9', $options2,$selected_opt,'id="dropu" size=2'); echo '<br/><br/>'; $this->load->view('select'); } } /* End of file select.php */ /* Location: ./application/controllers/select.php */ ?> |
Installation and testing: please download the source code from above link and extract the zip file and paste the application folder in you codeiginter folder ,it ask for replace the folder ,just replace and test the package with following URL in your local server. this url is without SEF htacces.
http://localhost/index.php/select/


