PHP recursive implementation of infinite classification PHP format array

WBOY
Release: 2016-07-25 08:54:11
Original
798 people have browsed it
  1. //I use mysql PDO for the database, but the whole idea is the same

  2. $conn=mysql_connect('localhost','root','123');
  3. if(mysql_errno()){
  4. printf('Connection failed'.mysql_error());
  5. }
  6. mysql_select_db('edeng');
  7. mysql_set_charset('utf8');
  8. /*
  9. *Recursive function
  10. *@param id To query all subclasses of fid=$id, the default value of $id is set to 0 because I set the fid of the top-level category to 0 in the database
  11. */
  12. function get_array($id=0){
  13. $sql="select id,fid,cname from e_cat where fid= $id";
  14. $result=mysql_query($sql);
  15. $arr=array();
  16. if($result && mysql_affected_rows()){
  17. while($rows=mysql_fetch_assoc($result)){

  18. $rows['child']=get_array($rows['id']);

  19. $arr[] = $rows;
  20. }
  21. return $arr;
  22. }
  23. }
  24. echo '
    ';
  25. $result = get_array();
  26. print_r($result);

  27. < ;/P>

  28. The function first queries all classes with fid 0

  29. Through the while loop one by one, the back investigation is performed to find the subclass whose fid is the id of the current class

Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!