I am populating information from a database to a drop down list on my home page. I have a table called "banks" with the respective column names "id" and "bankname". I also have another table called "bankbranches" with the respective column names "id","bankbranch" and "bank". The "bank" column in the bank branch has the same "id" of the "table". I want to run a query in my model to popupulate the bank branch based on the choice of the bank drop down list before it. So far, in my model I have:
function getBank()
{
$query = $this->db->query('SELECT id,bankname FROM banks');
return $query->result_array();
}
function getBankBranch()
{
$query = $this->db->query('SELECT id,bankbranch FROM bankbranches');
return $query->result_array();
But the bank branch drop down is listing all the bank branches in my database. I want the drop down list for the bank branch to be populated based on the selection of the bank drop down by the user. Here is my view:
<label>Bank</label>
<select name="banks" id="banks">
<option value="">--Please select--</option>
<?php
foreach($banks as $bank)
{
echo '<option value="'.$bank['id'].'">'.$bank['bankname'].'</option>';
}
?>
</select> <br/>
<label>Bank Branch</label>
<select name="bankbranches" id="bankbranches">
<option value=""></option>
<?php
foreach($bankbranch as $branch)
{
echo '<option value="'.$branch['id'].'">'.$branch['bankbranch'].'</option>';
}
?>
</select> <br/>
Here is my controller:
public function index()
{
$this->load->helper('form');
$data['cities'] = $this->site_model->getCity();
$data['modes'] = $this->site_model->getPaymentMode();
$data['banks'] = $this->site_model->getBank();
$data['bankbranch'] = $this->site_model->getBankBranch();
$data['categories'] = $this->site_model->getCategories();
$data['staffs'] = $this->site_model->getStaff();
$this->load->view('supplier_add',$data);
}
I have really tried to be elaborate.