如果不存在则创建表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
) ) 引擎=MyISAM 默认字符集=utf8 排序=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;
}
} ?>
您必须将此类文件保存在应用程序文件夹中的库文件夹中,文件名类别为