Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // create an array to hold the references
- $refs = array();
- // create and array to hold the list
- $list = array();
- $sql = "SELECT id, idparent, name, location FROM menu ORDER BY name";
- $result = $conn->query($sql);
- if ($result->num_rows > 0) {
- while ($row = $result->fetch_assoc()) {
- // Assign by reference
- $thisref = &$refs[ $row['id'] ];
- // add the the menu parent
- $thisref['idparent'] = $row['idparent'];
- $thisref['name'] = $row['name'];
- $thisref['location'] = $row['location'];
- // if there is no parent id
- if ($row['idparent'] == 0)
- {
- $list[ $row['id'] ] = &$thisref;
- }
- else
- {
- $refs[ $row['idparent'] ]['children'][ $row['id'] ] = &$thisref;
- }
- }
- } else {
- echo "0 results";
- }
- $conn->close();
- function create_menu( $arr )
- {
- $html = "\n<ul>\n";
- foreach ($arr as $key=>$val)
- {
- $html .= '<li><a href="'.$val['location'].'">'.$val['name']."</a>";
- if (array_key_exists('children', $val))
- {
- $html .= create_menu($val['children']);
- }
- $html .= "</li>\n";
- }
- $html .= "</ul>\n";
- return $html;
- }
- echo create_menu( $list );
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement