Home  >  Article  >  Backend Development  >  Code for infinite classification (recursion) implemented in PHP

Code for infinite classification (recursion) implemented in PHP

WBOY
WBOYOriginal
2016-07-25 09:03:291361browse
  1. $conn=mysql_connect("localhost","root","myoa888");

  2. mysql_select_db("lost63",$conn) or die("select db error");
  3. $sql[]="Create TABLE `lost63`.`dd_book_class` (
  4. `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  5. `classname` VARCHAR( 50 ) NOT NULL ,
  6. `classid` INT NOT NULL
  7. )";
  8. $sql[]="insert `dd_book_class` (classname,classid) values ('小说',0)";
  9. $sql[]="insert `dd_book_class` (classname,classid) values ('文艺',0)";
  10. $sql[]="insert `dd_book_class` (classname,classid) values ('青春',0)";
  11. $sql[]="insert `dd_book_class` (classname,classid) values ('励志/成功',0)";
  12. $sql[]="insert `dd_book_class` (classname,classid) values ('少儿',0)";
  13. $sql[]="insert `dd_book_class` (classname,classid) values ('悬疑',1)";
  14. $sql[]="insert `dd_book_class` (classname,classid) values ('言情',1)";
  15. $sql[]="insert `dd_book_class` (classname,classid) values ('职场',1)";
  16. $sql[]="insert `dd_book_class` (classname,classid) values ('文学',2)";
  17. $sql[]="insert `dd_book_class` (classname,classid) values ('科普',5)";
  18. //print_r($sql);
  19. /* -- insert test data -- */
  20. foreach($sql as $value){
  21. mysql_query($value) or die("insert data error");
  22. }

  23. $class_arr=array();

  24. $recordset=mysql_query("select * from `dd_book_class`");
  25. while($row=mysql_fetch_array($recordset)){
  26. $class_arr[]=array($row['id'],$row['classname'],$row['classid']);
  27. }
  28. function classtree($n,$id){
  29. global $class_arr;
  30. $n = str_pad('',$n,'-',STR_PAD_RIGHT);
  31. $n = str_replace("-"," ",$n);
  32. for($i=0;$iif($class_arr[$i][2]==$id){
  33. echo $n.'┠'.$class_arr[$i]
  34. [1].'
    ';
  35. classtree($n+1,$class_arr[$i][0]);
  36. }
  37. }
  38. }
  39. classtree(0,0);
  40. ?>

复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn