php 无极分类(递归)实现代码
程序员文章站
2022-06-12 07:58:50
复制代码 代码如下:
<?php
$conn=mysql_connect("localhost","root","myoa888");
mysql_select_db("lost63",$conn) or die("select db error");
$sql[]="create table `lost63`.`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);
/* -- insert test data -- */
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;
$n = str_pad('',$n,'-',str_pad_right);
$n = str_replace("-"," ",$n);
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+1,$class_arr[$i][0]);
}
}
}
classtree(0,0);
?>
复制代码 代码如下:
<?php
$conn=mysql_connect("localhost","root","myoa888");
mysql_select_db("lost63",$conn) or die("select db error");
$sql[]="create table `lost63`.`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);
/* -- insert test data -- */
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;
$n = str_pad('',$n,'-',str_pad_right);
$n = str_replace("-"," ",$n);
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+1,$class_arr[$i][0]);
}
}
}
classtree(0,0);
?>
上一篇: PHP 批量更新网页内容实现代码
下一篇: 处理php自动反斜杠的函数代码