The class of the formatted node tree is not well written and has no algorithm, but can be used

Source: Internet
Author: User
Format the classification data from the database, such as:

News
--Sports News
--Entertainment News
Financial
--Forex
--Financial
  1. Class Tree
  2. {
  3. /** Raw Data */
  4. Public $original;
  5. /**id's Key Name */
  6. public $id;
  7. /** the key name of the parent ID */
  8. Public $parentId;
  9. id*/at initialization of/**
  10. protected $initId;
  11. The level of the/** node */
  12. protected $thisLevel = 0;
  13. /** Final Tree */
  14. Protected $tree = Array ();
  15. /**
  16. * Constructor function
  17. +------------------------------------------
  18. * @access Public
  19. +------------------------------------------
  20. * @param array $original raw data
  21. * @param the key name of string $id ID
  22. * @param string $parentId The key name of the parent ID
  23. +------------------------------------------
  24. * @return void
  25. */
  26. Public function __construct ($original = ", $id =", $parentId = ")
  27. {
  28. if ($original && $id && $parentId)
  29. {
  30. $this->initialize ($original, $id, $parentId);
  31. }
  32. }
  33. /**
  34. * Initialization
  35. +------------------------------------------
  36. * @access Public
  37. +------------------------------------------
  38. * @param array $original raw data
  39. * @param the key name of string $id ID
  40. * @param string $parentId The key name of the parent ID
  41. +------------------------------------------
  42. * @return void
  43. */
  44. Public Function Initialize ($original, $id, $parentId)
  45. {
  46. $this->original = $original;
  47. $this->id = $id;
  48. $this->parentid = $parentId;
  49. }
  50. /**
  51. * Get initial node
  52. +----------------------------------------------
  53. * @access protected
  54. +----------------------------------------------
  55. * @param int $parentId The level of the initial node
  56. +----------------------------------------------
  57. * @return Array $parentTree
  58. */
  59. protected function Getparenttree ($parentId)
  60. {
  61. $parentTree = Array ();
  62. foreach ($this->original as $key = $value)
  63. {
  64. if ($value [$this->parentid] = = $parentId)
  65. {
  66. Array_push ($parentTree, $value);
  67. }
  68. }
  69. return $parentTree;
  70. }
  71. /**
  72. * Get subtree
  73. +----------------------------------------------
  74. * @access protected
  75. +----------------------------------------------
  76. * @param int $id The ID of the node
  77. * @param string $levelTag indent marker
  78. +----------------------------------------------
  79. * @return void
  80. */
  81. protected function Getchildrentree ($id, $levelTag)
  82. {
  83. foreach ($this->original as $key = $value)
  84. {
  85. if ($id = = $value [$this->parentid])
  86. {
  87. if ($LEVELTAG)
  88. {
  89. $this->getlevel ($value [$this->parentid]);
  90. $value [' leveltag '] = Str_repeat ($levelTag, $this->thislevel);
  91. $this->thislevel = 0;
  92. }
  93. $this->tree[] = $value;
  94. $this->getchildrentree ($value [$this->id], $levelTag);
  95. }
  96. }
  97. }
  98. /**
  99. * Get the level of the node
  100. +-------------------------------------------------
  101. * @access protected
  102. +-------------------------------------------------
  103. * @param int $PARENTID The parent ID of the node
  104. +-------------------------------------------------
  105. * @return void
  106. */
  107. protected function Getlevel ($parentId)
  108. {
  109. foreach ($this->original as $key = $value)
  110. {
  111. if ($parentId = = $value [$this->id] && $parentId! = $this->initid)
  112. {
  113. $this->thislevel++;
  114. $this->getlevel ($value [$this->parentid]);
  115. }
  116. }
  117. }
  118. /**
  119. * Get the full tree
  120. +-------------------------------------------------
  121. * @access Public
  122. +-------------------------------------------------
  123. * @param int $level from what level to start getting
  124. * @param string $levelTag indent marker
  125. +-------------------------------------------------
  126. * @return Array $this->tree complete tree
  127. */
  128. Public Function Gettree ($parentId =0, $levelTag = ")
  129. {
  130. $this->initid = $parentId;
  131. $parentTree = $this->getparenttree ($parentId);
  132. foreach ($parentTree as $key = $value)
  133. {
  134. $this->tree[] = $value;
  135. $this->getchildrentree ($value [$this->id], $levelTag);
  136. }
  137. return $this->tree;
  138. }
  139. }
  140. $conf = Array (
  141. 1 = = Array (' id ' = ' 1 ', ' ParentID ' =>0, ' name ' = ' 1 '),
  142. 2 = = Array (' id ' = ' 2 ', ' ParentID ' =>0, ' name ' = ' 2 '),
  143. 3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' 1-1 '),
  144. 4 = = Array (' id ' = ' 4 ', ' ParentID ' =>1, ' name ' = ' 1-2 '),
  145. 5 = = Array (' id ' = ' 5 ', ' ParentID ' =>2, ' name ' = ' 2-1 '),
  146. 6 = = Array (' id ' = ' 6 ', ' ParentID ' =>3, ' name ' = ' 1-1-1 '),
  147. 7 = = Array (' id ' = ' 7 ', ' ParentID ' =>4, ' name ' = ' 1-2-1 '),
  148. 8 = = Array (' id ' = ' 8 ', ' ParentID ' =>5, ' name ' = ' 2-1-1 '),
  149. 9 = = Array (' id ' = ' 9 ', ' ParentID ' =>8, ' name ' = ' 2-1-1-1 ')
  150. );
  151. $tree = new Tree ($conf, ' id ', ' parentid ');
  152. $arr = $tree->gettree (0, ");
  153. foreach ($arr as $val)
  154. {
  155. if ($val [' Leveltag '])
  156. {
  157. echo $val [' Leveltag ']. ' | - ';
  158. }
  159. echo $val [' name ']. '
    ';
  160. }
  161. ?>
Copy Code
  1. Class Tree
  2. {
  3. /** Raw Data */
  4. Public $original;
  5. /**id's Key Name */
  6. public $id;
  7. /** the key name of the parent ID */
  8. Public $parentId;
  9. id*/at initialization of/**
  10. protected $initId;
  11. The level of the/** node */
  12. protected $thisLevel = 0;
  13. /** Final Tree */
  14. Protected $tree = Array ();
  15. /**
  16. * Constructor function
  17. +------------------------------------------
  18. * @access Public
  19. +------------------------------------------
  20. * @param array $original raw data
  21. * @param the key name of string $id ID
  22. * @param string $parentId The key name of the parent ID
  23. +------------------------------------------
  24. * @return void
  25. */
  26. Public function __construct ($original = ", $id =", $parentId = ")
  27. {
  28. if ($original && $id && $parentId)
  29. {
  30. $this->initialize ($original, $id, $parentId);
  31. }
  32. }
  33. /**
  34. * Initialization
  35. +------------------------------------------
  36. * @access Public
  37. +------------------------------------------
  38. * @param array $original raw data
  39. * @param the key name of string $id ID
  40. * @param string $parentId The key name of the parent ID
  41. +------------------------------------------
  42. * @return void
  43. */
  44. Public Function Initialize ($original, $id, $parentId)
  45. {
  46. $this->original = $original;
  47. $this->id = $id;
  48. $this->parentid = $parentId;
  49. }
  50. /**
  51. * Get initial node
  52. +----------------------------------------------
  53. * @access protected
  54. +----------------------------------------------
  55. * @param int $parentId The level of the initial node
  56. +----------------------------------------------
  57. * @return Array $parentTree
  58. */
  59. protected function Getparenttree ($parentId)
  60. {
  61. $parentTree = Array ();
  62. foreach ($this->original as $key = $value)
  63. {
  64. if ($value [$this->parentid] = = $parentId)
  65. {
  66. Array_push ($parentTree, $value);
  67. }
  68. }
  69. return $parentTree;
  70. }
  71. /**
  72. * Get subtree
  73. +----------------------------------------------
  74. * @access protected
  75. +----------------------------------------------
  76. * @param int $id The ID of the node
  77. * @param string $levelTag indent marker
  78. +----------------------------------------------
  79. * @return void
  80. */
  81. protected function Getchildrentree ($id, $levelTag)
  82. {
  83. foreach ($this->original as $key = $value)
  84. {
  85. if ($id = = $value [$this->parentid])
  86. {
  87. if ($LEVELTAG)
  88. {
  89. $this->getlevel ($value [$this->parentid]);
  90. $value [' leveltag '] = Str_repeat ($levelTag, $this->thislevel);
  91. $this->thislevel = 0;
  92. }
  93. $this->tree[] = $value;
  94. $this->getchildrentree ($value [$this->id], $levelTag);
  95. }
  96. }
  97. }
  98. /**
  99. * Get the level of the node
  100. +-------------------------------------------------
  101. * @access protected
  102. +-------------------------------------------------
  103. * @param int $PARENTID The parent ID of the node
  104. +-------------------------------------------------
  105. * @return void
  106. */
  107. protected function Getlevel ($parentId)
  108. {
  109. foreach ($this->original as $key = $value)
  110. {
  111. if ($parentId = = $value [$this->id] && $parentId! = $this->initid)
  112. {
  113. $this->thislevel++;
  114. $this->getlevel ($value [$this->parentid]);
  115. }
  116. }
  117. }
  118. /**
  119. * Get the full tree
  120. +-------------------------------------------------
  121. * @access Public
  122. +-------------------------------------------------
  123. * @param int $level from what level to start getting
  124. * @param string $levelTag indent marker
  125. +-------------------------------------------------
  126. * @return Array $this->tree complete tree
  127. */
  128. Public Function Gettree ($parentId =0, $levelTag = ")
  129. {
  130. $this->initid = $parentId;
  131. $parentTree = $this->getparenttree ($parentId);
  132. foreach ($parentTree as $key = $value)
  133. {
  134. $this->tree[] = $value;
  135. $this->getchildrentree ($value [$this->id], $levelTag);
  136. }
  137. return $this->tree;
  138. }
  139. }
  140. $conf = Array (
  141. 1 = = Array (' id ' = ' 1 ', ' ParentID ' =>0, ' name ' = ' 1 '),
  142. 2 = = Array (' id ' = ' 2 ', ' ParentID ' =>0, ' name ' = ' 2 '),
  143. 3 = = Array (' id ' = ' 3 ', ' ParentID ' =>1, ' name ' = ' 1-1 '),
  144. 4 = = Array (' id ' = ' 4 ', ' ParentID ' =>1, ' name ' = ' 1-2 '),
  145. 5 = = Array (' id ' = ' 5 ', ' ParentID ' =>2, ' name ' = ' 2-1 '),
  146. 6 = = Array (' id ' = ' 6 ', ' ParentID ' =>3, ' name ' = ' 1-1-1 '),
  147. 7 = = Array (' id ' = ' 7 ', ' ParentID ' =>4, ' name ' = ' 1-2-1 '),
  148. 8 = = Array (' id ' = ' 8 ', ' ParentID ' =>5, ' name ' = ' 2-1-1 '),
  149. 9 = = Array (' id ' = ' 9 ', ' ParentID ' =>8, ' name ' = ' 2-1-1-1 ')
  150. );
  151. $tree = new Tree ($conf, ' id ', ' parentid ');
  152. $arr = $tree->gettree (0, ");
  153. foreach ($arr as $val)
  154. {
  155. if ($val [' Leveltag '])
  156. {
  157. echo $val [' Leveltag ']. ' | - ';
  158. }
  159. echo $val [' name ']. '
    ';
  160. }
  161. ?>
Copy Code
  • Related Article

    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.