Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. +Menu
  2. +Shop
  3. +Tshirts
  4. +Yellow
  5. +Green
  6. +Pants
  7. +Forum
  8. +Ideas
  9.  
  10. ----------------------------
  11. | id | parentID | text |
  12. |----|----------|----------|
  13. | 1 | null | Item #1 |
  14. | 2 | 5 | Item #2 |
  15. | 3 | 2 | Item #3 |
  16. | 4 | 2 | Item #4 |
  17. | 5 | null | Item #5 |
  18. | 6 | 5 | Item #6 |
  19. | 7 | 3 | Item #7 |
  20. | 8 | 5 | Item #8 |
  21. | 9 | 1 | Item #9 |
  22. | 10 | 7 | Item #10 |
  23. ----------------------------
  24.  
  25. $menu = array(
  26. 1 => array('text' => 'Item #1', 'parentID' => null),
  27. 2 => array('text' => 'Item #2', 'parentID' => 5),
  28. 3 => array('text' => 'Item #3', 'parentID' => 2),
  29. 4 => array('text' => 'Item #4', 'parentID' => 2),
  30. 5 => array('text' => 'Item #5', 'parentID' => null),
  31. 6 => array('text' => 'Item #6', 'parentID' => 5),
  32. 7 => array('text' => 'Item #7', 'parentID' => 3),
  33. 8 => array('text' => 'Item #8', 'parentID' => 5),
  34. 9 => array('text' => 'Item #9', 'parentID' => 1),
  35. 10 => array('text' => 'Item #10', 'parentID' => 7),
  36. );
  37.  
  38. <?php
  39. $addedAsChildren = array();
  40.  
  41. foreach ($menu as $id => &$menuItem) { // note that we use a reference so we don't duplicate the array
  42. if (!empty($menuItem['parentID'])) {
  43. $addedAsChildren[] = $id; // it should be removed from root, but we'll do that later
  44.  
  45. if (!isset($menu[$menuItem['parentID']]['children'])) {
  46. $menu[$menuItem['parentID']]['children'] = array($id => &$menuItem); // & means we use the REFERENCE
  47. } else {
  48. $menu[$menuItem['parentID']]['children'][$id] = &$menuItem; // & means we use the REFERENCE
  49. }
  50. }
  51.  
  52. unset($menuItem['parentID']); // we don't need parentID any more
  53. }
  54.  
  55. unset($menuItem); // unset the reference
  56.  
  57. foreach ($addedAsChildren as $itemID) {
  58. unset($menu[$itemID]); // remove it from root so it's only in the ['children'] subarray
  59. }
  60.  
  61. echo makeTree($menu);
  62.  
  63. function makeTree($menu) {
  64. $tree = '<ul>';
  65.  
  66. foreach ($menu as $id => $menuItem) {
  67. $tree .= '<li>' . $menuItem['text'];
  68.  
  69. if (!empty($menuItem['children'])) {
  70. $tree .= makeTree($menuItem['children']);
  71. }
  72.  
  73. $tree .= '</li>';
  74. }
  75.  
  76. return $tree . '</ul>';
  77. }
  78.  
  79. <ul><li>Item #1<ul><li>Item #9</li></ul></li><li>Item #5<ul><li>Item #2<ul><li>Item #3<ul><li>Item #7<ul><li>Item #10</li></ul></li></ul></li><li>Item #4</li></ul></li><li>Item #6</li><li>Item #8</li></ul></li></ul>
  80.  
  81. CREATE TABLE IF NOT EXISTS `categories` (
  82. `id` int(11) NOT NULL AUTO_INCREMENT,
  83. `name` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
  84. `parent` int(11) DEFAULT NULL,
  85. PRIMARY KEY (`id`)
  86. )
  87.  
  88. function parse($tree,$parent = 0,$level = 0){
  89. $level++;
  90. foreach ($tree as $p) {
  91. if ($p['parent'] != $parent) continue;
  92. $p['name'] = str_repeat('-',$level-1) . $p['name'];
  93. echo "<option name='cat' value='{$p['id']}'>{$p['name']}</option>";
  94. parse ($tree, $p['id'],$level);
  95. }
  96. }
  97.  
  98. $q = $link->query("SELECT * FROM `categories` ORDER BY name ASC");
  99. $cats = array();
  100. while($row = $q->fetch()){
  101. $cats[] = array("id"=>$row['id'],"name"=>$row['name'],"parent"=>$row['parent'],"level"=>0);
  102. }
  103.  
  104. parse($cats)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement