2013-09-27 62 views
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代碼將記錄添加到數據庫。

回答

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

輸出將被 Output Screen

+0

我會說有ID,名稱和PARENT_ID類別表,但是這只是一個命名約定......我們的想法是存儲父ID,因爲孩子只有一個父母但父母可能有多個孩子。 – Loenix

-2

在add.php頁面

<?php 
 
//--------------------------- 
 
//By MF alraii 2017 
 
//[email protected] 
 
//[email protected] 
 
//لا تنسونا من صالح الدعاء 
 
//--------------------------- 
 
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) { 
 
\t global $link; 
 
\t $link = mysqli_connect($db_hostname,$db_username,$db_password,$db_dbname); 
 
\t // اختبار الاتصال 
 
\t if (mysqli_connect_errno()) 
 
\t { 
 
\t \t die("خطأ في الاتصال: " . mysqli_connect_error()); 
 
\t } 
 
} 
 

 
function get_cat_selectlist($current_cat_id, $count, $lastname='') { 
 
\t 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"])) { 
 
\t global $link; 
 
\t $new_cat = $_POST["new_cat_name"]; 
 
\t $parent = $_POST["cat_id"]; 
 
\t $sql = "INSERT INTO categories (`id`, `name`, `parentid`) VALUES (NULL, '$new_cat', '$parent')"; 
 
    mysqli_query($link, $sql); 
 
\t 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 
 
//[email protected] 
 
//[email protected] 
 
//لا تنسونا من صالح الدعاء 
 
// الكود كاملا وأشمل في صفحة 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) { 
 
\t global $link; 
 
\t $link = mysqli_connect($db_hostname,$db_username,$db_password,$db_dbname); 
 
\t // اختبار الاتصال 
 
\t if (mysqli_connect_errno()) 
 
\t { 
 
\t \t die("Failed to connect to MySQL: " . mysqli_connect_error()); 
 
\t } 
 
} 
 

 
function get_cat_selectlist($current_cat_id, $count, $lastname='') { 
 
\t 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;

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