php實現樹狀結構無級分類_PHP教程

來源:互聯網
上載者:User
實現樹狀結構的兩種方法
1。遞迴法
遞迴是指在函數中顯式的調用它自身。
利用遞迴法實現樹狀結構的特點是寫入資料速度較快,顯示速度較慢(在樹的分支/層次較多的情況下尤其明顯)。適用與寫入資料量大,樹的結構複雜的情況下。
資料結構(以mysql為例)

代碼:--------------------------------------------------------------------------------
CREATE TABLE `tree1` (
`id` tinyint(3) unsigned NOT NULL auto_increment,
`parentid` tinyint(3) unsigned NOT NULL default '0',
`topic` varchar(50) default NULL,
PRIMARY KEY (`id`),
KEY `parentid` (`parentid`)
) TYPE=MyISAM;

INSERT INTO `tree1` (`id`, `parentid`, `topic`) VALUES
(1,0,'樹1'),
(2,0,'樹2'),
(3,0,'樹3'),
(4,2,'樹2-1'),
(5,4,'樹2-1-1'),
(6,2,'樹2-2'),
(7,1,'樹1-1'),
(8,1,'樹1-2'),
(9,1,'樹1-3'),
(10,8,'樹1-2-1'),
(11,7,'樹1-1-1'),
(12,11,'樹1-1-1-1');
--------------------------------------------------------------------------------


欄位說明
id,記錄的id號
parentid,記錄的父記錄id(為0則為根記錄)
topic,記錄的顯示標題

顯示程式

順序樹:

PHP代碼:--------------------------------------------------------------------------------

/* 資料庫連接 */
mysql_connect();
mysql_select_db('tree');

/* 樹狀顯示的遞迴函式 */
function tree($parentid = 0) {
/*執行sql查詢,擷取記錄的標題和id*/
$sql = "select topic,id from tree1 where parentid = $parentid order by id asc";
$rs = mysql_query($sql);
/* 縮排*/
echo("
    ");
    while($ra = mysql_fetch_row($rs)) {
    /* 顯示記錄標題 */
    echo('
  • '.$ra[0].'
  • ');
    /* 遞迴調用 */
    tree($ra[1]);
    }
    echo("
");
}
tree();
?>

--------------------------------------------------------------------------------


逆序樹:

PHP代碼:--------------------------------------------------------------------------------

/* 資料庫連接 */
mysql_connect();
mysql_select_db('tree');

/* 樹狀顯示的遞迴函式 */
function tree($parentid = 0) {
/*執行sql查詢,擷取記錄的標題和id*/
$sql = "select topic,id from tree1 where parentid = $parentid order by id desc";
$rs = mysql_query($sql);
/* 縮排*/
echo("
    ");
    while($ra = mysql_fetch_row($rs)) {
    /* 顯示記錄標題 */
    echo('
  • '.$ra[0].'
  • ');
    /* 遞迴調用 */
    tree($ra[1]);
    }
    echo("
");
}
tree();
?>

--------------------------------------------------------------------------------


插入資料程式

PHP代碼:--------------------------------------------------------------------------------

/* 資料庫連接 */
mysql_connect();
mysql_select_db('tree');
$sql = "insert into tree (topic,parentid) values('樹3-1',3);";
mysql_query($sql);
?>

http://www.bkjia.com/PHPjc/631398.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631398.htmlTechArticle實現樹狀結構的兩種方法 1。遞迴法 遞迴是指在函數中顯式的調用它自身。 利用遞迴法實現樹狀結構的特點是寫入資料速度較快,顯示速度...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.