Infinite pole Classification of PHP

Source: Internet
Author: User
Tags php basics php framework vars java se

First set up the classification information table:

[SQL]View Plaincopy
  1. CREATE TABLE IF not EXISTS ' category ' (
  2. ' CategoryId ' smallint (5) unsigned not NULL auto_increment,
  3. ' ParentID ' smallint (5) unsigned not NULL DEFAULT ' 0 ',
  4. ' CategoryName ' varchar (not NULL) ,
  5. PRIMARY KEY (' categoryId ')
  6. )  ;


Insert several data:

[SQL]View Plaincopy
  1. INSERT into ' category ' (' categoryId ', ' parentid ', ' CategoryName ') VALUES
  2. (1, 0, ' php '),
  3. (2, 0, ' java '),
  4. (3, 0, ' C + + '),
  5. (4, 1, ' PHP basics '),
  6. (5, 1, ' PHP open source data '),
  7. (6, 1, ' PHP framework '),
  8. (7, 2, ' Java Se '),
  9. (8, 2, ' Java EE '),
  10. (9, 2, ' Java Me '),
  11. (3, ' C + + BASIC programming '),
  12. (One, 3, ' development of the C + + system '),
  13. (3, ' C embedded Programming '),
  14. (3, ' C + + application development '),
  15. ( +, ' C + + desktop application development '),
  16. ( D, ' C + + game development ');


Here is the PHP code:

[PHP]View Plaincopy
    1. <?php
    2. /*
[PHP]View Plaincopy
    1. PHP Infinite Pole classification
[PHP]View Plaincopy
  1. */
  2. Get the direct subcategory of a category
  3. function Getsons ($categorys,$catId =0) {
  4. $sons =Array ();
  5. foreach ($categorys as $item) {
  6. if ($item [' ParentID ']==$catId)
  7. $sons []=$item;
  8. }
  9. return $sons;
  10. }
  11. Get all subcategories of a category
  12. function Getsubs ($categorys,$catId =0,$level =1) {
  13. $subs =Array ();
  14. foreach ($categorys as $item) {
  15. if ($item [' ParentID ']==$catId) {
  16. $item [' Level ']=$level;
  17. $subs []=$item;
  18. $subs =array_merge ($subs, Getsubs ($categorys,$item [' categoryId '],$level + 1));
  19. }
  20. }
  21. return $subs;
  22. }
  23. Get all the parent categories for a category
  24. Method One, recursive
  25. function getparents ($categorys,$catId) {
  26. $tree =Array ();
  27. foreach ($categorys as $item) {
  28. if ($item [' categoryId ']==$catId) {
  29. if ($item [' ParentID ']>0)
  30. $tree =array_merge ($tree, getparents ($categorys,$item [' ParentID ']);
  31. $tree []=$item;
  32. Break ;
  33. }
  34. }
  35. return $tree;
  36. }
  37. Method Two, iteration
  38. function getParents2 ($categorys,$catId) {
  39. $tree =Array ();
  40. While ($catId! = 0) {
  41. foreach ($categorys as $item) {
  42. if ($item [' categoryId ']==$catId) {
  43. $tree []=$item;
  44. $catId =$item [' ParentID '];
  45. Break ;
  46. }
  47. }
  48. }
  49. return $tree;
  50. }
  51. Test section
  52. $pdo =New PDO (' mysql:host=localhost;dbname=test ',' root ', '8888 ');
  53. $stmt =$pdo->query ("SELECT * from category ORDER by CategoryId");
  54. $categorys =$stmt->fetchall (PDO::FETCH_ASSOC);
  55. $result =getsons ($categorys, 1);
  56. foreach ($result as $item)
  57. echo $item [' CategoryName '].'  <br> ';
  58. echo '
  59. $result =getsubs ($categorys, 0);
  60. foreach ($result as $item)
  61. echo str_repeat (',$item [' Level ']). $item [' CategoryName '].'  <br> ';
  62. echo '
  63. $result =getparents ($categorys, 7);
  64. foreach ($result as $item)
  65. echo $item [' CategoryName '].' >> ';
  66. echo '
  67. $result =getparents2 ($categorys, 15);
  68. foreach ($result as $item)
  69. echo $item [' CategoryName '].' >> ';
  70. ?>


Here is the result of the run:

Article Source: http://blog.csdn.net/kankan231/article/details/8462349

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.