tree($ra[1]);
}
echo("</ul>");
}
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("<ul>");
while($ra = mysql_fetch_row($rs)) {
/* 显示记录标题 */
echo('<li>'.$ra[0].'</li>');
/* 递归调用 */
tree($ra[1]);
}
echo("</ul>");
}
tree();
?>
插入数据程序
PHP:
<?
/* 数据库连接 */
mysql_connect();
mysql_select_db('tree');
$sql = "insert into tree (topic,parentid) values('树3-1',3);";
mysql_query($sql);
?>
2.排序字段法
此方法是通过在数据结构中增加一个标志记录在整个树中的顺序位置的字段来实现的。特点是显示速度和效率高。但在单个树的结构复杂的情况下,数据写入效率有所不足。而且顺序排列时候,插入,删除记录的算法过于复杂,故通常用逆序排列。
数据结构(以mysql为例)
代码:
CREATE TABLE `tree2` (
`id` tinyint(3) unsigned NOT NULL auto_increment,
`parentid` tinyint(3) unsigned NOT NULL default '0',
`rootid` tinyint(3) unsigned NOT NULL default '0',
`layer` tinyint(3) unsigne
| 对此文章发表了评论 |
