Advertisement
cdsatrian

Nested List Mysql

Oct 22nd, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.39 KB | None | 0 0
  1. <?php
  2. /*
  3. --
  4. -- Database: `test`
  5. --
  6. -- --------------------------------------------------------
  7. --
  8. -- Table structure for table `nested_categories`
  9. --
  10. DROP TABLE IF EXISTS `nested_categories`;
  11. CREATE TABLE IF NOT EXISTS `nested_categories` (
  12.   `id_cat` int(11) NOT NULL AUTO_INCREMENT,
  13.   `name` varchar(20) NOT NULL,
  14.   `lft` int(11) NOT NULL,
  15.   `rgt` int(11) NOT NULL,
  16.   PRIMARY KEY (`id_cat`)
  17. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
  18. --
  19. -- Dumping data for table `nested_categories`
  20. --
  21. INSERT INTO `nested_categories` (`id_cat`, `name`, `lft`, `rgt`) VALUES
  22. (1, 'Website', 1, 22),
  23. (2, 'Database', 2, 9),
  24. (3, 'Oracle', 3, 4),
  25. (4, 'MySQL', 5, 6),
  26. (5, 'SQL Server', 7, 8),
  27. (6, 'Programming', 10, 19),
  28. (7, 'JavaScript', 11, 14),
  29. (8, 'jQuery', 12, 13),
  30. (9, 'PHP', 15, 16),
  31. (10, 'CSS', 17, 18),
  32. (11, 'HTML', 20, 21);
  33. */
  34. mysql_connect('localhost','root','');
  35. mysql_select_db('test');
  36. $sql="SELECT node.name, (COUNT( parent.name ) -1) AS depth
  37.      FROM nested_categories AS node
  38.        CROSS JOIN nested_categories AS parent
  39.      WHERE node.lft BETWEEN parent.lft AND parent.rgt
  40.      GROUP BY node.name
  41.      ORDER BY node.lft";
  42. $result = mysql_query($sql);
  43. $tree = array();
  44. while ($row = mysql_fetch_assoc($result)) {
  45.     $tree[] = $row;
  46. }
  47. // Bootstrap loop
  48. $result        = '';
  49. $currDepth     = 0;
  50. $lastNodeIndex = count($tree) - 1;
  51. // Start the loop
  52. foreach ($tree as $index => $currNode) {
  53.     // Level down? (or the first)
  54.     if ($currNode['depth'] > $currDepth || $index == 0) {
  55.         $result .= '<ul>';
  56.     }
  57.     // Level up?
  58.     if ($currNode['depth'] < $currDepth) {
  59.         $result .= str_repeat('</ul></li>', $currDepth - $currNode['depth']);
  60.     }
  61.     // Always open a node
  62.     $t = ($index == 0) ? 1 : 2;
  63.     $result .= '<li>' . $currNode['name'];
  64.     // Check if there's chidren
  65.     if ($index != $lastNodeIndex && $tree[$index + 1]['depth'] <= $tree[$index]['depth']) {
  66.         $result .= '</li>'; // If not, close the <li>
  67.     }
  68.     // Adjust current depth
  69.     $currDepth = $currNode['depth'];
  70.     // Are we finished?
  71.     if ($index == $lastNodeIndex) {
  72.         $result .= '</ul>' . str_repeat('</li></ul>', $currDepth);
  73.     }
  74. }
  75.  
  76. // Indent the code
  77. // For UTF8: tidy_parse_string($result, array('indent' => true, 'show-body-only' => true), 'UTF8')
  78. //$result = tidy_parse_string($result, array('indent' => true, 'show-body-only' => true));
  79. print $result;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement