0

如何使用 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 代码来向数据库添加记录。

4

2 回答 2

1

创建表

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

输出将是 输出画面

于 2014-03-13T06:34:06.687 回答
-2

在 add.php 页面中

<?php
//---------------------------
//By MF alraii 2017
//aseer333@hotmail.com
//mfalraii@gmail.com
//لا تنسونا من صالح الدعاء
//---------------------------
error_reporting(E_ALL);
ini_set('display_errors', 1);

$db_hostname = "localhost";
$db_username = "";
$db_password = "";
$db_dbname = "";

$link = false;

Connect($db_hostname,$db_username,$db_password,$db_dbname);
function Connect($db_hostname,$db_username,$db_password,$db_dbname) {
	global $link;
	$link = mysqli_connect($db_hostname,$db_username,$db_password,$db_dbname);
	// اختبار الاتصال
	if (mysqli_connect_errno())
	{
		die("خطأ في الاتصال: " . mysqli_connect_error());
	}
}

function get_cat_selectlist($current_cat_id, $count, $lastname='') {
	global $link;
    static $option_results;
    // اذا لم يكن هناك مجموعه معرفه سابقا يبدأ من هنا 
    if (!isset($current_cat_id)) {
        $current_cat_id=1;
    }
    // زيادة العداد برقم 1
    $count = $count+1;

    // استعلام قاعدة البيانات للفئات الفرعية مهما كانت الفئة الأم
    $sql =  "SELECT id, name from categories where parentid =  ".$current_cat_id." order by name asc";

    $get_options = mysqli_query($link, $sql);
    $num_options = mysqli_num_rows($get_options);

    // وضع القسم في المكان المناسب له في الشجرة 
    if ($num_options > 0) {
        while (list($cat_id, $cat_name) = mysqli_fetch_row($get_options)) {

        // اذا تم حذف القسم الاساسي ولم يوجد 
        //اظهر هذا في القسم الابن او القسم الفرعي

        if ($current_cat_id!=0) {
            $indent_flag =  $lastname . '--';
            $indent_flag .=  '>';
        }
            $cat_name = $indent_flag.$cat_name;
            $option_results[$cat_id] = $cat_name;
            // الان استدعاء الدالة مرة اخرى واعادة بناء القسم الجديد في موقعه الصحيح 
            get_cat_selectlist($cat_id, $count, $cat_name);
        }
    }
    return $option_results;
}

// اختبار اذا كان هناك قسم جديد مسجل لادراجه في شجرة او تفرعات الاقسام
if (isset($_POST["new_cat_name"])) {
	global $link;
	$new_cat = $_POST["new_cat_name"];
	$parent = $_POST["cat_id"];
	$sql = "INSERT INTO categories (`id`, `name`, `parentid`) VALUES (NULL, '$new_cat', '$parent')";
    mysqli_query($link, $sql);
	echo "التصنيف الجديد باسم :  '$new_cat'قد تمت اضافته للاقسام!<br><br>";
}

echo '<form action="add.php" method="post">';
echo '<label>تصنيف جديد باسم: </label> <input type="text" name="new_cat_name"></input><br><br>';
echo '<label>التصنيف الاساسي: </label>';
echo '<select name="cat_id">';
echo '<option value="0">-- قسم جديد -- </option><br><br>';

$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\"";
        // عرض العنصر المحدد كما تم تحديدة في القائمة
        if ($_POST['cat_id'] == "$key") {
            $options .=" selected=\"selected\"";
        }
        $options .=">$value</option>\n";
    }
}
echo $options;
echo '</select><br><br>';  
echo '<input type="submit" value="اضافة قسم جديد"></input></form>';

?>

并在 index.php 页面中

<?php

//---------------------------
//By MF alraii 2017
//aseer333@hotmail.com
//mfalraii@gmail.com
//لا تنسونا من صالح الدعاء
// الكود كاملا وأشمل في صفحة add.php
//صفحة الاندكس فقط لعرض النتيجة
//---------------------------

$db_hostname = "localhost";
$db_username = "";
$db_password = "";
$db_dbname = "";

Connect($db_hostname,$db_username,$db_password,$db_dbname);
function Connect($db_hostname,$db_username,$db_password,$db_dbname) {
	global $link;
	$link = mysqli_connect($db_hostname,$db_username,$db_password,$db_dbname);
	// اختبار الاتصال
	if (mysqli_connect_errno())
	{
		die("Failed to connect to MySQL: " . mysqli_connect_error());
	}
}

function get_cat_selectlist($current_cat_id, $count, $lastname='') {
	global $link;
    static $option_results;
     // اذا لم يكن هناك مجموعه معرفه سابقا يبدأ من هنا 
    if (!isset($current_cat_id)) {
        $current_cat_id=1;
    }
    // زيادة العداد برقم 1
    $count = $count+1;

    // استعلام قاعدة البيانات للفئات الفرعية مهما كانت الفئة الأم
    $sql =  "SELECT id, name from categories where parentid =  ".$current_cat_id." order by name asc";

    $get_options = mysqli_query($link, $sql);
    $num_options = mysqli_num_rows($get_options);

   // وضع القسم في المكان المناسب له في الشجرة 
    if ($num_options > 0) {
        while (list($cat_id, $cat_name) = mysqli_fetch_row($get_options)) {

         // اذا تم حذف القسم الاساسي ولم يوجد 
        //اظهر هذا في القسم الابن او القسم الفرعي

        if ($current_cat_id!=0) {
            $indent_flag =  $lastname . '--';
            $indent_flag .=  '>';
        }
            $cat_name = $indent_flag.$cat_name;
            $option_results[$cat_id] = $cat_name;
            // الان استدعاء الدالة مرة اخرى واعادة بناء القسم الجديد في موقعه الصحيح 
            get_cat_selectlist($cat_id, $count, $cat_name);
        }
    }
    return $option_results;
}

echo '<select name="cat_id">';

$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\"";
         // عرض العنصر المحدد كما تم تحديدة في القائمة
        if ($_POST['cat_id'] == "$key") {
            $options .=" selected=\"selected\"";
        }
        $options .=">$value</option>\n";
    }
}
echo $options;
echo '</select>';  

并在数据库中

sql文件导入

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+03:00";




CREATE TABLE `categories` (
  `id` int(11) NOT NULL,
  `name` varchar(37) COLLATE utf8_unicode_ci NOT NULL,
  `parentid` int(11) DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;



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),
(27, 'test1111', 5),
(28, 'testmf', 5),
(29, 'testmof', 1);


ALTER TABLE `categories`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `id` (`id`),
  ADD KEY `parentid_fk` (`parentid`);


ALTER TABLE `categories`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=30;

ولا تنسونا من صالح الدعاء

于 2017-08-24T19:40:28.710 回答