[eluser]cideveloper[/eluser]
ok so some modifications have been made but I think you can figure it out after this. What happens is add_new gets all brands but only the categories for brand_id = 1. The javascript looks for a change event and sends a get request to get_cat which gets the categories for the specific id changed to. Its not perfect but works well.
Controller
Code:
function add_new(){
$brand_id = 1;
$data["title"]="Add Product";
$data["brandList"]=$this->brand_model->get_data();
$data["categoryList"]=$this->category_model->get_category($brand_id);
$data["main_content"]="product/add";
$this->load->view("ci176981",$data);
}
function get_cat($brand_id=null) {
$data["categoryList"] = $this->category_model->get_category($brand_id)->result();
echo json_encode($data);
}
Category_model
Code:
function get_category($brand_id=null){
if($brand_id) {
$this->db->where('brand_id',$brand_id);
}
return $this->db->get('tbl_category');
}
Brand_model
Code:
function get_data(){
$query=$this->db->get('tbl_brand');
return $query;
}
View
Code:
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8" />
<title><?=$title;?></title>
<link rel="stylesheet" href="<?=base_url()?>assets/css/site.css" />
</head>
<body>
<div class="field">
<label for="brand_id">Brand Available</label>
<select name="brand_id" id="brand_id" class="medium">
<?php foreach($brandList->result() as $row): ?>
<option value="<?=$row->id;?>"><?=$row->name;?></option>
<?php endforeach; ?>
</select>
</div>
<div class="field">
<label for="category_id">Category Available</label>
<select name="category_id" id="category_id" class="medium">
<?php foreach($categoryList->result() as $row): ?>
<option value="<?=$row->id;?>"><?=$row->name;?></option>
<?php endforeach; ?>
</select>
</div>
[removed][removed]
[removed][removed]
</body>
</html>
Javascript
Code:
$(function(){
$('#brand_id').change(function(e){
$.get('get_cat/'+$(this).val(), function(data) {
var new_cat_list = ""
for (var i=0; i<data.categoryList.length; i++)
{
new_cat_list += '<option value="'+data.categoryList[i].id+'">'+data.categoryList[i].name+'</option>';
}
$('#category_id').html(new_cat_list);
}, 'json');
});
});