Advertisement
rfv123

Q29910284-display-multilevel-database-driven-menu

Apr 28th, 2015
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.77 KB | None | 0 0
  1. <?php // https://stackoverflow.com/questions/29910284/display-multilevel-database-driven-menu-in-php
  2. //  use functions...
  3.  
  4.  /*
  5.   * We will use Mysqli
  6.   */
  7.  
  8. $DB_HOST     = "localhost";
  9. $DB_USER     = "test";
  10. $DB_PASSWORD = "test";
  11. $DB_TO_USE   = "testmysql";
  12.  
  13. $dbc = new mysqli($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_TO_USE);
  14.  
  15. /** -----------------------------------------------------------------
  16.  * Select all the menu entries for a given entry Id
  17.  *
  18.  * Note: it is safe to do 'parameter substitution' rather than using
  19.  *       'prepared queries' with placeholders as this 'entry Id' never
  20.  *       comes from an external source.
  21.  *
  22.  * @param mysqli $dbc
  23.  * @param integer $currentEntryId
  24.  * @return array of menu entries
  25.  */
  26. function selectSubMenu($currentEntryId, $dbc)
  27. {
  28.     $sql = "SELECT cat_id, cat_name, cat_parent_id
  29.                   FROM catalogue
  30.                   WHERE cat_parent_id = {$currentEntryId}
  31.                   ORDER BY cat_id";
  32.     $resultSet = mysqli_query($dbc, $sql);
  33.     return $resultSet->fetch_all(MYSQLI_ASSOC);
  34. }
  35.  
  36. /** --------------------------------------------------------------------------
  37.  * Process the current menu enty - it will return a complete menu as HTML
  38.  *
  39.  * These needs to know whether the current entry has a submenu and
  40.  * will therefore need to generate an 'unordered list' for the current entry.
  41.  *
  42.  * Alas, it also has to not display the 'list item text' for the  'root' entry
  43.  * but process the 'submenu'.
  44.  * This complicates the <li> current entry text generation slightly.
  45.  *
  46.  * @param array $currentEntry - one row
  47.  * @param array $subMenu - many rows - may be empty
  48.  * @param mysqli $dbc - database connection
  49.  * @return string - the HTML for the menu
  50.  */
  51. function processMenuEntry($currentEntry, array $subMenu, $dbc)  {
  52.     $outMenu = '';
  53.     $isRoot = $currentEntry['cat_id'] == 0; // do 'First time' processing
  54.  
  55.     // display the current entry text as a 'list item'
  56.     $outMenu .= !$isRoot ? "<li><a href='#'>" . $currentEntry['cat_name'] . "</a>" : '';
  57.  
  58.     // does it have a submenu...
  59.     if (!empty($subMenu)) { // we need to add a comlete submenu of <ul> ... </ul>
  60.  
  61.         // Start of the submenu as an unordered list -- decide what is required
  62.         if ($isRoot) {
  63.             $outMenu .= '<ul class="dropdown">';
  64.         }
  65.         else {
  66.             $outMenu .= '<ul>';
  67.         }
  68.  
  69.         // Display each entry of the submenu as a 'list item' ... wait...
  70.         // We already have a routine to do that - 'processMenuEntry'...
  71.  
  72.         foreach ($subMenu as $submenuEntry) {
  73.             $outMenu .= processMenuEntry($submenuEntry,
  74.                                     selectSubMenu($submenuEntry['cat_id'], $dbc),
  75.                                     $dbc);
  76.         }
  77.  
  78.         // close the current submenu - terminate the unordered list
  79.         $outMenu .= '</ul>';
  80.     }
  81.  
  82.     // terminate the current list item
  83.     $outMenu .= !$isRoot ? '</li>' : '';
  84.     return $outMenu;
  85. };
  86.  
  87. /* -------------------------------------------------------------------
  88.  * Process all the menu entries
  89.  *
  90.  * We need a complete menu 'tree' that includes a 'root' which is not provided
  91.  * in the database. I think it should be. Whatever, i need one.
  92.  *
  93.  * Initializing a 'tree walk' i always find 'interesting' ;-/
  94.  */
  95. $root = array('cat_id' => 0, 'cat_name' => '', 'cat_parent_id' => 0);
  96.  
  97. // build the output menu
  98. $outMenu = processMenuEntry($root,
  99.                            selectSubMenu($root['cat_id'], $dbc),
  100.                            $dbc);
  101.  
  102. // wrap it in a <div>
  103. $htmlMenu = '<div style="border: 1px solid red">'
  104.             . $outMenu
  105.             .'</div>';
  106. ?>
  107. <!DOCTYPE html>
  108. <html>
  109. <head>
  110.     <title>Test Recursive Menu Builder</title>
  111. </head>
  112. <body>
  113. <?= $htmlMenu ?>
  114. </body>
  115. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement