检索分层数据从表中UL L1标签显示树状结构
我有如下表:检索分层数据从表中UL L1标签显示树状结构
id name lft rgt level
--- ------------------------- ---- ---- -----
1 company name 1 16 0
2 HR 2 3 1
3 Superwiser 4 9 1
4 Associates 5 6 2
5 test 10 13 1
6 test2 11 12 2
使用这个数据库,我想显示UL标签里的树结构。但没有得到这张桌子。我要显示这样的:
1. Company Name
|--:Hr
|--:Superwiser
|--:Associates
|--:test
|--:test2
我怎么能火,这和如何UL L1标签来显示它特定的查询。提前致谢。
<?php
类分类01{
var $table='';
var $CI ='';
var $ul_class='';
function Category($config=array()){
$this->table=$config['table'];
$this->CI=& get_instance();
$this->CI->load->database();
$this->ul_class=$config['ul_class'];
}
function getTree($parent_id=0){
$this->CI->db->where('parent_id',$parent_id);
$first_level=$this->CI->db->get('category')->result();
$tree= '<ul class="'.$this->ul_class.'">';
foreach($first_level as $fl){
$tree.='<li>'.$fl->name;
$this->CI->db->where('parent_id',$fl->cat_id);
$count=$this->CI->db->count_all_results($this->table);
if($count!=0){
$tree.=$this->getTree($fl->cat_id);
}
$tree.= '</li>';
}
$tree.= '</ul>';
return $tree;
}
} ?>
使用这个库
你能否澄清上面的代码,你在这里做了什么。 – Sky 2012-07-25 06:54:09
兄弟你的数据库结构是错误的..应该有一个父域ID的字段。我将向你展示一个例子。 – 2012-07-27 06:11:55
递归调用数据库是魔鬼的迹象,所以我的方式通常处理这,它自己使用PHP。
即
function buildTree($ParentID, $Categories, $Output = '')
{
foreach($Categories as $Category)
{
// Skip any categories that are not
// in the current parent.
if($Category->ParentID != $ParentID)
continue;
// Add the current category to the output
$Output .= '<li>' . $Category->name . '</li>';
// If the category has children, recurse another level.
if($Category->ChildCount > 0)
{
$Output .= '<ul>';
$Output .= $this->buildTree($Category->ID, $Categories, $Output);
$Output .= '</ul>';
}
}
return $Output;
}
,那么只需在调用它:
<ul><?= buildTree(0, $Categories); ?></ul>
的代码依赖于你做一个子查询,返回属于该父行子行的计。
即
select *, (select count(C2.*) from Category C2 where C2.ParentID = C1.ID) as ChildCount from Category as C1
这就可以让你只能继续递归如果父节点实际上有孩子,防止空UL
的被添加到输出的时候没有孩子存在。
CREATE TABLE IF NOT EXISTS category
( cat_id
INT(11)NOT NULL AUTO_INCREMENT, name
VARCHAR(255)COLLATE utf8_bin DEFAULT NULL, image
VARCHAR(255)COLLATE utf8_bin DEFAULT NULL, parent_id
INT(11)NOT NULL DEFAULT '0', top
TINYINT(1)NOT NULL, column
INT(3)NOT NULL, sort_order
INT(3)NOT NULL DEFAULT '0', status
TINYINT(1)NOT NULL, total_product
INT( 11)NOT NULL, date_added
INT(11)NOT NULL, date_modified
INT(11)NOT NULL, PRIMARY KEY(cat_id
) )ENGINE = MyISAM的默认字符集= UTF8 COLLATE = utf8_bin AUTO_INCREMENT = 17;
这是我的数据库表,并即时将与此表 创建树这里是我的功能
function getTree(){
$config['ul_class']='tree';
$config['table']='category';
$this->load->library('category',$config);
echo $this->category->getTree();
}
和图书馆一样
<?php
类分类01{
var $table='';
var $CI ='';
var $ul_class='';
function Category($config=array()){
$this->table=$config['table'];
$this->CI=& get_instance();
$this->CI->load->database();
$this->ul_class=$config['ul_class'];
}
function getTree($parent_id=0){
$this->CI->db->where('parent_id',$parent_id);
$first_level=$this->CI->db->get('category')->result();
$tree= '<ul class="'.$this->ul_class.'">';
foreach($first_level as $fl){
$tree.='<li>'.$fl->name;
$this->CI->db->where('parent_id',$fl->cat_id);
$count=$this->CI->db->count_all_results($this->table);
if($count!=0){
$tree.=$this->getTree($fl->cat_id);
}
$tree.= '</li>';
}
$tree.= '</ul>';
return $tree;
}
} ?>
你必须在应用程序文件夹保存此类文件的库文件夹里面有文件名类
你能解释一下你的方案给我吗?通常在树结构中,通常只有一个“ParentID”列将该行链接到它所属的另一行。如果你有这个,根据你的输出没有多大意义 – Gavin 2012-07-25 08:34:48