本文介绍了CodeIgniter中的类别和子类别的链选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在CI中创建一个下拉菜单,如果我选择一个类别,子类别将出现在同一页面上,而不刷新浏览器?

解决方案

这是我做的,(很容易做)


  1. 下载jQuery,

  2. 插入jQuery + jquery。

  3. 创建用于从数据库生成选择的函数

  4. 调用函数

我的表结构如下(我知道可以在一个表中轻松完成)



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),
',1);

子类别

  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` `)
)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,'21世纪',1),
(6,4,'18-20世纪',1),
(7,4,'15-17世纪',1),
(8,4,'15世纪前, );

ALTER TABLE`subcategory`
ADD CONSTRAINT`subcategory_ibfk_1` FOREIGN KEY(`category_id')REFERENCES`category`(`id`);

现在,我只需加载所有活动类别/子类别, c $ c>< select> s。



请注意,我使用twitter-bootstrap 2,因此有一些额外的 HTML

$ b注意,我使用自己的 MY_Controller 文件扩展 CI_Controller ,因此我可以设置global $ data [ $ this-> data ['key'] 到exted的(传递给查看 CI_Controller遵循此。 p>

注意,它内置了重新填充功能,所以每当你通过(有效或无效) category_id&& ||

 <$ c> $ subategory_id 如果参数是正确的,它会在DB中查找,如果存在则返回cateogry / subcategory。 $ c> 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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ,$ 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\>。 < / 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 $ categories){

$ 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;

}



最后创建 general_model.php 并创建此函数

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

最后一步是以一种方式调用函数,我们需要。
在所需的控制器只需调用 $ this-> category_chain(),并且在视图中将有一个变量avalible $ category_chain (只是回应<?= $ category_chain?>



;)


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?

解决方案

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 ;)

这篇关于CodeIgniter中的类别和子类别的链选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 18:20