How to add sub category on my codeigniter php application?
I have a view
echo "<label for='parent'>Category</label><br/> ";
echo form_dropdown('category_id', $categories). "<p>";
cont开发者_StackOverflow社区roller
function create(){
if($this->input->post('name')){
$this->MProducts->addProduct();
$this->session->set_flashdata('message', 'Products Created');
redirect('admin/products/index', 'refresh');
}else{
$data['title'] = "Create Product";
$data['main'] = 'admin_product_create';
$data['categories']= $this->MCats->getTopCategories();
$this->load->vars($data);
$this->load->view('dashboard');
}
}
and the model is
function getTopcategories(){
$data = array();
$data[0] = 'root';
$this->db->where('parentid',0);
$Q = $this->db->get('categories');
if($Q->num_rows() > 0){
foreach($Q->result_array() as $row){
$data[$row['id']] = $row['name'];
}
}
$Q->free_result();
return $data;
}
Basically, what i want is we get sub categories when we click categories and 'selected' subcategory id goes to database when we create products, So that we can list products by sub categories. Please help me how do we do that ?
You should do it via AJAX and retrieve the subcategories. Have a ready subcategories <select>
or dropdown.
Append the values return by PHP into the dropdown.
Some pseudo-code:
$.each(data, function(key, value)
{
$('#subcategories').
append($("<option></option>").attr("value",key).text(value));
});
精彩评论