Categories 如何添加无限深度类别、子类别和项目

Categories 如何添加无限深度类别、子类别和项目,categories,depth,Categories,Depth,如何使用Codeigniter添加无限深度类别、子类别和项目 Category 1 -Sub cat -Sub cat --Sub sub cat --- Sub sub cat -- Sub sub cat -Sub cat Category 2 -Sub cat -Sub cat --Sub sub cat ---sub sub sub cat ----sub sub sub sub cat -sub cat Category 3 -Sub cat 我需要我的SQL数据库和PHP代码或Co

如何使用Codeigniter添加无限深度类别、子类别和项目

Category 1
-Sub cat
-Sub cat
--Sub sub cat
--- Sub sub cat
-- Sub sub cat
-Sub cat Category 2
-Sub cat
-Sub cat
--Sub sub cat
---sub sub sub cat
----sub sub sub sub cat
-sub cat Category 3
-Sub cat
我需要我的SQL数据库和PHP代码或Codeigiter代码来向数据库添加记录。

创建表

CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(37) COLLATE utf8_unicode_ci NOT NULL,
`parentid` int(11) DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `parentid_fk` (`parentid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;

INSERT INTO `categories` (`id`, `name`, `parentid`) VALUES
(1, 'animal', 0),
(2, 'vegetable', 0),
(3, 'mineral', 0),
(4, 'doggie', 1),
(5, 'kittie', 1),
(6, 'horsie', 1),
(7, 'gerbil', 0),
(8, 'birdie', 1),
(9, 'carrot', 2),
(10, 'tomato', 2),
(11, 'potato', 2),
(12, 'celery', 2),
(13, 'rutabaga', 2),
(14, 'quartz', 3),
(15, 'feldspar', 3),
(16, 'silica', 3),
(17, 'gypsum', 3),
(18, 'hunting', 4),
(19, 'companion', 4),
(20, 'herding', 4),
(21, 'setter', 18),
(22, 'terrier', 18),
(23, 'poodle', 19),
(24, 'chihuahua', 19),
(25, 'shepherd', 20),
(26, 'collie', 20);
php代码

// $current_cat_id: the current category id number
// $count: just a counter, call it as 0 in your function call and forget about it
/* GET THE DROP DOWN LIST OF CATEGORIES */
function get_cat_selectlist($current_cat_id, $count, $lastname='') {
    static $option_results;
    // if there is no current category id set, start off at the top level (zero)
    if (!isset($current_cat_id)) {
        $current_cat_id=1;
    }
    // increment the counter by 1
    $count = $count+1;

    // query the database for the sub-categories of whatever the parent category is
    $sql =  "SELECT id, name from categories where parentid =  ".$current_cat_id." order by name asc";

    $get_options = mysql_query($sql);
    $num_options = mysql_num_rows($get_options);

    // our category is apparently valid, so go ahead €¦
    if ($num_options > 0) {
        while (list($cat_id, $cat_name) = mysql_fetch_row($get_options)) {

        // if its not a top-level category, indent it to
        //show that its a child category

        if ($current_cat_id!=0) {
            $indent_flag =  $lastname . '--';
//          for ($x=2; $x<=$count; $x++) {
                $indent_flag .=  '>';
//          }
        }
            $cat_name = $indent_flag.$cat_name;
            $option_results[$cat_id] = $cat_name;
            // now call the function again, to recurse through the child categories
            get_cat_selectlist($cat_id, $count, $cat_name);
        }
    }
    return $option_results;
}
echo '<select name="cat_id">';
echo '<option value="">-- Select -- </option>';

$get_options = get_cat_selectlist(0, 0);
if (count($get_options) > 0){
    $categories = $_POST['cat_id'];
    foreach ($get_options as $key => $value) {
        $options .="<option value=\"$key\"";
        // show the selected items as selected in the listbox
        if ($_POST['cat_id'] == "$key") {
            $options .=" selected=\"selected\"";
        }
        $options .=">$value</option>\n";
    }
}
echo $options;
echo '</select>';   
输出将是

在add.php页面中


我会说一个带有id、name和parent_id的category表,但这只是一个命名约定。。。其思想是存储父id,因为一个子项只有一个父项,但一个父项可以有多个子项。