Php recursive implementation of infinite classification php formatted array-php Tutorial

Source: Internet
Author: User
Php recursion achieves unlimited classification php formatted array

  1. // The mysql PDO used by the database is the same as the whole idea.

  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 functions
  10. * @ Param id: to query all fid = $ id sub-classes, set the default value of $ id 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. The function first queries all classes whose fid is 0.

  28. Call back one by one while to find the subclass whose fid is the id of the current class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.