Answers for "create dynamic dropdown on codeigniter"

0

create dynamic dropdown on codeigniter

CREATE DYNAMIC DROPDOWN IN CONDEIGNITER
---------------------------
BASE MODEL
---------------------------
public function convertObjectToArray($data) {
	if (is_object($data)) {
		$data = get_object_vars($data);
	}

	if (is_array($data)) {
		// return array_map(__FUNCTION__, $data);
		return array_map(array($this, 'convertObjectToArray'), $data);
	}

	return $data;
}
public function generate_dropdownOption($query){
	$query = $this->db->query($query);
    $results =$this->convertObjectToArray($query->getResult());
    $html = '';
    foreach ($results as $row){
    	row = array_values($row);
    	$html .='<option value="'.$row[0].'">'.ucwords($row[1]).'</option>';
	}
	return  $html;
}

public function getDropdown_sex(){
	$query = "SELECT sex_ID,sex_Name FROM  `ref_sex`";
	return $this->generate_dropdownOption($query);
}
public function getDropdown_marital(){
	$query = "SELECT marital_ID,marital_Name FROM  `ref_marital`";
	return $this->generate_dropdownOption($query);
}
---------------------------
CONTROLLER
---------------------------

public function HomePage()
{
	$pageData = [
    	'dropdown' =>[
			'sex'=> $this->baseModel->getDropdown_sex(),
			'marital'=> $this->baseModel->getDropdown_marital(),
        ]
    ];
    
    return view('your_viewfile_here',$pageData);
}

---------------------------
VIEW:
---------------------------
<select class="form-control" id="profile_sex" name="profile_sex" required="">
   <?php echo $this->data['dropdown']['sex']?>
</select>

----------

If you want to update it the dropdown without loading, 
just use ajax to call route for getting dropdown :)

[Support Links!]
Ko-fi: https://ko-fi.com/rhalp10
Paypal:  https://paypal.me/rhalp10
Posted by: Guest on May-12-2021

Browse Popular Code Answers by Language