How to add sub category on my codeigniter php application ?
- by sagarmatha
Hello friends
I have a view
echo "<label for='parent'>Category</label><br/> ";
echo form_dropdown('category_id', $categories). "<p>";
controller
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 ?