我有下表:
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 li标签中显示树结构。但没有得到这张桌子。我想这样显示:
1. Company Name
|--:Hr
|--:Superwiser
|--:Associates
|--:test
|--:test2
如何为此触发特定查询以及如何在ul li标记中显示它。提前谢谢。
答案 0 :(得分:1)
<?php
类别 {
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;
}
} ?&GT;
使用此库
答案 1 :(得分:1)
CREATE TABLE IF NOT 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,
主要关键(cat_id
)
)ENGINE = MyISAM DEFAULT CHARSET = 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
类别 {
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;
}
} ?&GT;
您必须将此类文件保存在具有文件名类别
的应用程序文件夹中的库文件夹中答案 2 :(得分:0)
递归数据库调用是恶魔的标志,所以我通常处理它的方式是自己使用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
添加到输出中。