Chain select with category and sub category in CodeIgniter

后端 未结 3 1360
别那么骄傲
别那么骄傲 2020-12-22 02:47

How to create a dropdown select menu in CI so that if I select a category, a sub category will appear on the same page without refreshing the browser?

相关标签:
3条回答
  • 2020-12-22 02:49

    Actually this is the code I want to make it happen for, I want children to appear when parent is selected.

    if(count($navlist)){
    
    foreach($navlist as $key => $list){
        echo "<select>";
            foreach($list as $topkey => $toplist){
                echo '<option value="'. $topkey .'">'. $toplist["name"]. '</option>';
    
    
                    if(count($toplist['children'])){
                        /*foreach($toplist['children'] as $subkey => $subname){
                            echo "\n<li class='subcat'>";
                            echo anchor("welcome/cat/$subkey", $subname);
                            echo "</li>";
                }       */
            }
        }   
    }
    
    echo "</select>";
    }
    
    ?>
    

    Thanks

    0 讨论(0)
  • 2020-12-22 02:50

    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)); 
    });
    
    0 讨论(0)
  • 2020-12-22 03:12

    This is how I've done it, (very easy to do)

    1. download jQuery, + jquery.chained.js
    2. insert jQuery + jquery.chained.js to the CI view(s)
    3. create function for generating selects from database
    4. calling the function

    as far my structure of tables is as following (I know it can be easily done in one table)

    category

    CREATE TABLE IF NOT EXISTS `category` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(64) NOT NULL,
      `active` tinyint(1) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;
    
    INSERT INTO `category` (`id`, `name`, `active`) VALUES
    (3, 'Science', 1),
    (4, 'History', 1);
    

    subcategory

    CREATE TABLE IF NOT EXISTS `subcategory` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `category_id` int(11) NOT NULL,
      `name` varchar(64) NOT NULL,
      `active` int(11) NOT NULL,
      PRIMARY KEY (`id`),
      KEY `category_id` (`category_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;
    
    INSERT INTO `subcategory` (`id`, `category_id`, `name`, `active`) VALUES
    (2, 3, 'Mathematics', 1),
    (3, 3, 'Physics', 1),
    (4, 3, 'Medicine', 1),
    (5, 4, '21st Century', 1),
    (6, 4, '18-20th Century', 1),
    (7, 4, '15-17th Century', 1),
    (8, 4, 'Before 15th Century', 1);
    
    ALTER TABLE `subcategory`
      ADD CONSTRAINT `subcategory_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`);
    

    Now I just load all the active categories/subcategories and create <select>s for both of them.

    Note that I am using twitter-bootstrap 2 so there is some extra HTML.

    Note that I am extending CI_Controller with my own MY_Controller file therefore I can set "global" $data[] (that is passed to view) by doing $this->data['key'] to exted CI_Controller follow this tutorial.

    Note that it has built in "repopulate feature" so whenever you pass (valid or invalid) category_id && || $subcategory_id it looks in the DB if parameters are correct and cateogry/subcategory exists if so it repopulates itself.

    public function category_chain($category_id = FALSE, $subcategory_id = FALSE) { 
        $this->load->model('general_model');
    
        $repopulate['category'] = ''; 
        $repopulate['subcategory'] = ''; 
    
        if (($category_id !== FALSE && $subcategory_id !== FALSE) || ($category_id !== "FALSE" && $subcategory_id !== "FALSE")) { 
    
            if ($this->general_model->_isInDBWhere('subcategory', array('id' => $subcategory_id, 'category_id' => $category_id))) { 
    
                $repopulate['category'] = $category_id; 
                $repopulate['subcategory'] = $subcategory_id; 
    
            } 
    
        } 
    
        if (($category_id !== FALSE && $subcategory_id === FALSE) || ($category_id !== "FALSE" && $subcategory_id === "FALSE")) { 
    
            if ($this->general_model->_isInDB('category', 'id', $category_id)) { 
    
                $repopulate['category'] = $category_id; 
                $repopulate['subcategory'] = ''; 
    
            } 
    
        } 
    
        $categories = $this->general_model->_getAllWhere('category', 'active', '1'); 
        $subcategories = $this->general_model->_getAllWhere('subcategory', 'active', '1'); 
    
        $return = "<div class=\"control-group\"> 
                    <label class=\"control-label\">.category </label> 
                        <div class=\"controls\"> 
                        <div class=\"input-prepend\"> 
                                <span class=\"add-on\"><i class=\"icon-th-large\"></i></span>"; 
        $return .= "<select name=\"category_id\" id=\"category\">"; 
        $return .= "<option value=\"\">--</option>"; 
    
        foreach ($categories as $category) { 
    
            $return .= "<option value=\"".$category->id."\"".(($repopulate['category'] == $category->id) ? " selected": "").">".$category->name."</option>"; 
    
        } 
    
        $return .= "</select>"; 
        $return .= "</div></div></div>"; 
    
    
        $return .= "<div class=\"control-group\"> 
                    <label class=\"control-label\">.subcategory </label> 
                        <div class=\"controls\"> 
                        <div class=\"input-prepend\"> 
                                <span class=\"add-on\"><i class=\"icon-th\"></i></span>"; 
        $return .= "<select name=\"subcategory_id\" id=\"subcategory\">"; 
        $return .= "<option value=\"\">--</option>"; 
    
        foreach ($subcategories as $subcategory) { 
    
            $return .= "<option value=\"".$subcategory->id."\" class=\"".$subcategory->category_id."\"".(($repopulate['subcategory'] == $subcategory->id) ? " selected": "").">".$subcategory->name."</option>"; 
    
        } 
    
        $return .= "</select>"; 
        $return .= "</div></div></div>"; 
    
        $this->data['category_chain'] = $return; 
    
    }
    

    lastly create application/models/general_model.php and create this function

    function _getAllWhere($table, $where, $value) { 
            $q = $this->db->get_where($table, array($where => $value)); 
            return ($q->num_rows() > 0) ? $q->result() : FALSE; 
    } 
    

    Lasts step is to call the function in a way that it is called just when we need it. In desired controller just call $this->category_chain() and in view there will be a variable avalible $category_chain (just echo it out like <?=$category_chain?>)

    Thats it ;)

    0 讨论(0)
提交回复
热议问题