PHP infinite classification of recursive implementation code

  • 2020-03-31 20:17:40
  • OfStack


<?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 (' A novel ',0)"; 
$sql[]="insert `dd_book_class` (classname,classid) values (' Literature and art ',0)"; 
$sql[]="insert `dd_book_class` (classname,classid) values (' youth ',0)"; 
$sql[]="insert `dd_book_class` (classname,classid) values (' motivational / successful ',0)"; 
$sql[]="insert `dd_book_class` (classname,classid) values (' Children's ',0)"; 
$sql[]="insert `dd_book_class` (classname,classid) values (' The suspense ',1)"; 
$sql[]="insert `dd_book_class` (classname,classid) values (' romance ',1)"; 
$sql[]="insert `dd_book_class` (classname,classid) values (' In the workplace ',1)"; 
$sql[]="insert `dd_book_class` (classname,classid) values (' literature ',2)"; 
$sql[]="insert `dd_book_class` (classname,classid) values (' The popular science ',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; 
$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); 
?>


Related articles: