PHP Infinitus-php Tutorial

Source: Internet
Author: User
PHP Infinitus classification code

  1. // Obtain all descendant classes without recursion
  2. Function get_offspring ($ pids, $ list)
  3. {
  4. $ Npid = array ();
  5. $ Offspring = array ();
  6. $ Has_child = true;
  7. While ($ has_child)
  8. {
  9. $ Has_child = false;
  10. Foreach ($ list as $ lk => $ lv)
  11. {
  12. If (in_array ($ lv ['pid'], $ pids ))
  13. {
  14. $ Offspring [] = $ lv;
  15. $ Npid [] = $ lv ['CID'];
  16. Unset ($ list [$ lk]);
  17. $ Has_child = true;
  18. }
  19. }
  20. $ Pids = $ npid;
  21. }
  22. Return $ offspring;
  23. }
  24. // Use the path field to obtain the generation class
  25. Function get_offspring ($ pid)
  26. {
  27. $ Offspring = array ();
  28. $ Cats = $ this-> getList ('cat _ id, cat_name, parent_id, cat_path ', array (), 0,-1 );
  29. Foreach ($ cats as $ cv)
  30. {
  31. If (in_array ($ pid, explode (',', $ cv ['cat _ path'])
  32. {
  33. $ Offspring [] = $ cv;
  34. }
  35. }
  36. Return $ offspring;
  37. }
  38. // Update the classification path of the later generation
  39. Function update_offspring_path ($ pid, $ ppath)
  40. {
  41. If ($ ppath = ',')
  42. {
  43. $ Ppath = '';
  44. }
  45. $ Offspring = $ this-> get_offspring ($ pid );
  46. Foreach ($ offspring as $ ov)
  47. {
  48. $ Ov ['cat _ path'] = substr ($ ov ['cat _ path'], 0, strlen ($ ov ['cat _ path'])-1 );
  49. $ Old_path = explode (',', $ ov ['cat _ path']);
  50. Foreach ($ old_path as $ oldk => $ oldv)
  51. {
  52. If ($ oldv = $ pid)
  53. {
  54. Break;
  55. }
  56. Unset ($ old_path [$ oldk]);
  57. }
  58. $ New_path = $ ppath. implode (',', $ old_path ).',';
  59. If (! $ This-> update (array ('cat _ path' => $ new_path), array ('cat _ id' => $ ov ['cat _ id'])
  60. {
  61. Return false;
  62. }
  63. }
  64. Return true;
  65. }
  66. // Organize the list into a tree
  67. Function get_tree_list ($ pid, $ arr, & $ r)
  68. {
  69. Foreach ($ arr as $ k => $ v)
  70. {
  71. If ($ v ['parent _ id'] = $ pid)
  72. {
  73. If (isset ($ r [$ pid]) // you can specify a flag containing a subclass.
  74. {
  75. $ R [$ pid] ['has _ child '] = 1;
  76. }
  77. $ V ['cat _ path'] = trim ($ v ['cat _ path']); // calculate the depth
  78. $ Path_str = substr ($ v ['cat _ path'], 0, strlen ($ v ['cat _ path'])-1 );
  79. If ($ path_str = '')
  80. {
  81. $ V ['deep '] = 0;
  82. }
  83. Else
  84. {
  85. $ V ['deep '] = count (explode (', ', $ path_str ));
  86. }
  87. $ V ['format'] = str_repeat ('', 6 * $ v ['deep ']); // calculate indentation
  88. $ R [$ v ['cat _ id'] = $ v; // add to the ordered list
  89. Unset ($ arr [$ k]); // added to remove from unordered list
  90. $ This-> get_tree_list ($ v ['cat _ id'], $ arr, $ r );
  91. }
  92. }
  93. }


Infinitus, PHP

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.