Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. <?php
  2. // create an array to hold the references
  3. $refs = array();
  4.  
  5. // create and array to hold the list
  6. $list = array();
  7.  
  8. $sql = "SELECT id, idparent, name, location FROM menu ORDER BY name";
  9.  
  10. $result = $conn->query($sql);
  11. if ($result->num_rows > 0) {
  12. while ($row = $result->fetch_assoc()) {
  13. // Assign by reference
  14. $thisref = &$refs[ $row['id'] ];
  15.  
  16. // add the the menu parent
  17. $thisref['idparent'] = $row['idparent'];
  18. $thisref['name'] = $row['name'];
  19. $thisref['location'] = $row['location'];
  20.  
  21. // if there is no parent id
  22. if ($row['idparent'] == 0)
  23. {
  24. $list[ $row['id'] ] = &$thisref;
  25. }
  26. else
  27. {
  28. $refs[ $row['idparent'] ]['children'][ $row['id'] ] = &$thisref;
  29. }
  30. }
  31. } else {
  32. echo "0 results";
  33. }
  34.  
  35. $conn->close();
  36.  
  37. function create_menu( $arr )
  38. {
  39. $html = "\n<ul>\n";
  40. foreach ($arr as $key=>$val)
  41. {
  42. $html .= '<li><a href="'.$val['location'].'">'.$val['name']."</a>";
  43. if (array_key_exists('children', $val))
  44. {
  45. $html .= create_menu($val['children']);
  46. }
  47. $html .= "</li>\n";
  48. }
  49. $html .= "</ul>\n";
  50. return $html;
  51. }
  52.  
  53. echo create_menu( $list );
  54. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement