php简单的无级分类

//递归实现的无极分类
$con=@mysql_connect('127.0.0.1','root','root') or die(mysql_error());

mysql_select_db('test') or die('数据库连接失败');

//$sql[]="Create TABLE `test`.`dd_book_class` (
//`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
//`classname` VARCHAR( 50 ) NOT NULL ,
//`classid` INT NOT NULL
//)";
//$sql[]="insert `dd_book_class` (classname,classid) values ('小说',0)";
//$sql[]="insert `dd_book_class` (classname,classid) values ('文艺',0)";
//$sql[]="insert `dd_book_class` (classname,classid) values ('青春',0)";
//$sql[]="insert `dd_book_class` (classname,classid) values ('励志/成功',0)";
//$sql[]="insert `dd_book_class` (classname,classid) values ('少儿',0)";
//$sql[]="insert `dd_book_class` (classname,classid) values ('悬疑',1)";
//$sql[]="insert `dd_book_class` (classname,classid) values ('言情',1)";
//$sql[]="insert `dd_book_class` (classname,classid) values ('职场',1)";
//$sql[]="insert `dd_book_class` (classname,classid) values ('文学',2)";
//$sql[]="insert `dd_book_class` (classname,classid) values ('科普',5)";
//print_r($sql);
//foreach($sql as $value){
// mysql_query($value) or die("insert data error");
//}

$class_arr=array();
$recordset=mysql_query("select * from `dd_book_class`");
while($row=mysql_fetch_array($recordset)){
$class_arr[]=array($row['id'],$row['classname'],$row['classid']);
}

function classTree($n,$id){
global $class_arr;
for($i=0;$i<count($class_arr);$i++){
if($class_arr[$i][2]==$id){
echo $n.'┠<a href="?class='.$class_arr[$i][0].'">'.$class_arr[$i][1].'</a><br>';
classtree($n.'&nbsp;&nbsp;&nbsp;&nbsp;',$class_arr[$i][0]);
}
}
}
classtree('',0);
mysql_close($con);
posted @ 2017-06-17 13:52  几番春秋  阅读(1200)  评论(0编辑  收藏  举报