'Populate Dynamic Dropdowns List in Codeigniter
Database inlcude Groups like Arts,Science and IT.By Choosing one Group Second Dynamic Dropdown appear which will show the Subjects Related to Group. This is my View
<?php include('admin_header.php'); ?>
<body id="page-wrapper">
<div class="container-fluid">
<h1 class="text-center">Enter Courses</h1>
<hr>
</div>
<?php if($feedback = $this->session->flashdata('feedback')):
$feedback_class = $this->session->flashdata('feedback_class');
?>
<div class="col-lg-6 col-lg-offset-3">
<div class="alert alert-dismissible <?= $feedback_class ?>">
<button type="button" class="close" data-dismiss="alert">×</button>
<p><?= $feedback ?></p>
</div>
</div>
<?php endif; ?>
<div class="cotainer-fluid">
<div class="col-lg-6 col-lg-offset-3">
<?php echo form_open('courses/input',['class'=>'form-group']); ?>
<br>
<?php echo form_input(['name'=>'rollno','class'=>'form-control','placeholder'=>'Enter Student ROll NO']); ?>
<br>
<select name="group" class="form-control" onchange="/courses/getsubject'+this.value" >
<option value="Arts">Arts</option>
<option value="Science">Science</option>
<option value="IT">IT</option>
</select>
<br>
<?php echo form_dropdown(''); ?>
<br>
<?php echo form_submit(['class'=>'form-control btn btn- success','value'=>'Submit']) ?>
<?php echo form_close(); ?>
</div>
</div>
</body>
And This is Controller
<?php
class Courses extends MY_Controller
{
public function assign()
{
$this->load->helper('form');
$this->load->view('admin/course');
}
public function getsubject($grp)
{
$this->load->model('courses');
$grp = $this->courses->getsubjects($grp);
}}
?>
I didnt know how to do this in Codeigniter,Got helped from google like Ajax trickds but nothing
Solution 1:[1]
Here is an example:
Controller(Index)
<?php
class Index extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url_helper');
$this->load->model('countries_model');
}
public function index() {
$query = $this->db->query('SELECT id,name FROM countries');
$data['countries'] = $query->result_array();
$this->load->view('index/index', $data);
}
public function cities() {
$country_id = $this->input->post('country_id');
$query = $this->db->query('SELECT id,name FROM cities WHERE country_id=' . $country_id);
$data['cities'] = $query->result_array();
$this->load->view('index/cities', $data);
echo $country_id;
}
}
Model(Countries_model.php)
<?php
class Countries_model extends CI_Model {
public function __construct() {
$this->load->database();
}
}
View(index.php)
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dymanic Selects</title>
<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery.js" ></script>
<script type="text/javascript">
$(document).ready(function(){
$('#sl_countries').change(function(){
$('#sl_cities').load("<?php echo site_url('index/cities') ?>",{country_id:$(this).val()});
});
});
</script>
</head>
<body>
<label>Countries</label>
<select id="sl_countries">
<?php foreach($countries as $c):?>
<option value="<?php echo $c['id']?>"><?php echo $c['name']?></option>
<?php endforeach;?>
</select>
<label>Cities</label>
<select id="sl_cities"></select>
</body>
</html>
View (cities.php)
<?php foreach ($cities as $c): ?>
<option value="<?php echo $c['id'] ?>"><?php echo $c['name'] ?></option>
<?php endforeach; ?>
To do this I used jquery, so it's easier. It's necessary two views, index.php is the main view and cities.php is the view to show the list of countries.
Solution 2:[2]
just send array and get on view page.
<select id="sl_countries">
<?php foreach($countries as $c):?>
<option value="<?php echo $c['id']?>"><?php echo $c['name']?></option>
<?php endforeach;?>
</select>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Alexander Ceballos |
Solution 2 | Noor Fahad |