Dynamic ComboBox With CodeIgniter

I’m updating a project that I created using CodeIgniter. I have to add a combobox (select) to a registration form that contains state names. I did not want to create a static combo box, so I created a states table in the MySQL database that I’m using for the project. The states table contains three fields (state_id, state_abbrev, state_name).

The Model

I first created a states model. I have already populated the states table, so I only need to create methods to retrieve the data. The first method will be used to retrieve a specific row from the states table based upon the state_id. The second method will retrieve all values from the states table. This second method will be used to populate the combobox.

class mStates extends Model {
    	function mStates() {
    		parent::Model();
	}	
 
	function getState($id) {
		$data = array();
		$options = array('state_id' => $id);
		$Q = $this->db->getwhere('states', $options, 1);
		if ($Q->num_rows() > 0) {
			$data = $Q->row_array();	
		}	
		$Q->free_result();
		return $data;
	}
 
	function getStates() {
		$data = array();
		$Q = $this->db->get('states');
		if ($Q->num_rows() > 0) {
			foreach ($Q->result_array() as $row){
		         	$data[] = $row;
		        }
		}	
		$Q->free_result();
		return $data;	
	}
}

The getStates method queries the states table, loops through the data and stores it in an array.

The Controller

This is an existing project so I had previously created a register method in one of my controllers. I only need to add line to retrieve the states data from the mStates model.

function register(){
	 if ($this->input->post('email')){
	 	$this->mMembers->addMember();
		$this->session->set_flashdata('message','Account created');
		redirect('members/login','refresh');
	} else {
		$data['title'] = $this->title;
		$data['caption'] = "Member Registration";
		$data['navList'] = $this->mainMenu;
		$data['page'] = 'register';
		$data['tour'] = $this->mTours->getLatestTour();
                // retrieve the states and add to the data array
		$data['states'] = $this->mStates->getStates();
		$this->load->vars($data);
		$this->load->view('main');
	}
}

The View

I created a register view with a form that enables users to register with the site. I need to add a combobox and populate the data dynamically.

echo form_open('members/register');
echo "<p><label for='first_name'>First Name</label><span class='required'>*</span><br/>";
$data = array('name'=>'first_name','id'=>'first_name','maxlength'=>20);
echo form_input($data) ."</p>";
 
echo "<p><label for='last_name'>Last Name</label><span class='required'>*</span><br/>";
$data = array('name'=>'last_name','id'=>'last_name','maxlength'=>20);
echo form_input($data) ."</p>";
 
echo "<p><label for='u'>Email</label><span class='required'>*</span><br/>";
$data = array('name'=>'email','id'=>'u','maxlength'=>50);
echo form_input($data) ."</p>";
 
echo "<p><label for='state'>State</label><span class='required'>*</span><br/>";
echo "<select name='state_id' id='state_id'>";
if (count($states)) {
	foreach ($states as $key => $list) {
		echo "<option value='". $list['state_id'] . "'>" . $list['state_name'] . "</option>";
	}		
}	
echo "</select>";
 
echo form_submit('submit','register');
echo form_close();

If the states array has values, loop through each row and add it to the combobox. That’s it. As you can see using CodeIgniter to populate a combobox is not that difficult.

Be Sociable, Share!

Checkout My New Site - T-shirts For Geeks