PHP : Create menu from SQL Server database
<p>How can I create a dynamic menu created in a database?</p>

<p>A sample of the menu table :</p>

<pre><code>ID  NAME        URL         IDPARENT
----------------------------------
1   Accueil     #Accueil    0         
2   Parcs       #Parcs      0         
3   Allemagne   #Allemagne  2         
4   Berlin      #Berlin     3          
5   France      #France     2           
6   Contact     #Contact    0    
</code></pre>

<p>The result should be :</p>

<pre><code>&lt;ul&gt; 
 &lt;li&gt;Accueil&lt;/li&gt;
 &lt;li&gt;Parcs&lt;/li&gt;
    &lt;ul&gt;
     &lt;li&gt;Allemagne&lt;/li&gt;
        &lt;ul&gt;
          &lt;li&gt;Berlin&lt;/li&gt;
        &lt;/ul&gt;  
     &lt;li&gt;France&lt;/li&gt;
   &lt;/ul&gt;
 &lt;li&gt;Contact&lt;/li&gt;
&lt;/ul&gt;
</code></pre>

<p>SOLVED : My code :</p>

<pre><code>        &lt;?php
        //connection to the database
        $dbhandle = mssql_connect('*****', '*****', '*****')
          or die("Couldn't connect to Server");     
        //select a database to work with
         $selected = mssql_select_db("*****", $dbhandle)
          or die("Couldn't open database"); 
          //declare the SQL statement that will query the database
        $query = "SELECT * FROM CATEGORIES ";
        //execute the SQL query and return records
        $result = mssql_query($query);
        //display the results 
        while($row = mssql_fetch_array($result))   
                {
                // Assign by reference
                $thisref = &amp;$refs[ $row['ID'] ];
                // add the the menu parent
                $thisref['IDCategoriePere'] = $row['IDCategoriePere'];
                $thisref['NOM'] = $row['NOM'];
                $thisref['URL'] = $row['URL'];
                // if there is no parent id
                if ($row['IDCategoriePere'] == 0)
                {
                    $list[ $row['ID'] ] = &amp;$thisref;
                }
                else
                {
                    $refs[ $row['IDCategoriePere'] ]['children'][ $row['ID'] ] = &amp;$thisref;
                }
        } 
        function create_menu( $arr )
            {
                $html = "\n&lt;ul&gt;\n";
                foreach ($arr as $key=&gt;$val) 
                {
                    $html .= '&lt;li&gt;&lt;a href="'.$val['URL'].'"&gt;'.$val['NOM']."&lt;/a&gt;&lt;/li&gt;\n";
                    if (array_key_exists('children', $val))
                    {
                        $html .= create_menu($val['children']);
                    }
                }
                $html .= "&lt;/ul&gt;\n";
                return $html;
            }
            echo create_menu( $list );
            //close the connection
         mssql_close($dbhandle);
         ?&gt;
</code></pre>

<p>It works fine ! but when i tried to put css (<a href="http://cssmenumaker.com/menu/flat-jquery-responsive-menu" rel="nofollow noreferrer">http://cssmenumaker.com/menu/flat-jquery-responsive-menu</a>), the dropdown doesn't show :(</p>

<p>Result with and witout css :</p>

<pre><code>  &lt;style&gt;
/* CSS Document */
@import url(http://fonts.googleapis.com/css?family=Open+Sans);
@import url(http://fonts.googleapis.com/css?family=Bree+Serif);
#container {
margin: 0 auto;
}
nav {
margin: 50px 0;
background-color: #E64A19;
}
nav ul {
padding: 0;
margin: 0;
list-style: none;
position: relative;
}
nav ul li {
display:inline-block;
background-color: #E64A19;
}
nav a {
display:block;
padding:0 10px; 
color:#FFF;
font-size:20px;
line-height: 60px;
text-decoration:none;
}
nav a:hover { 
background-color: #000000; 
}
/* Hide Dropdowns by Default */
nav ul ul {
display: none;
position: absolute; 
top: 60px; /* the height of the main nav */
}   
/* Display Dropdowns on Hover */
nav ul li:hover &gt; ul {
display:inherit;
}   
/* Fisrt Tier Dropdown */
nav ul ul li {
width:170px;
float:none;
display:list-item;
position: relative;
}
/* Second, Third and more Tiers */
nav ul ul ul li {
position: relative;
top:-60px; 
left:170px;
}
/* Change this in order to change the Dropdown symbol */
li &gt; a:after { content:  ' +'; }
li &gt; a:only-child:after { content: ''; }
&lt;/style&gt;

&lt;!-- WITH PHP --&gt;
&lt;div id="container"&gt;
&lt;nav&gt;
&lt;?php
        //connection to the database
        $dbhandle = mssql_connect('*****', '*****', '*****')
          or die("Couldn't connect to Server");     
        //select a database to work with
         $selected = mssql_select_db("*****", $dbhandle)
          or die("Couldn't open database"); 
          //declare the SQL statement that will query the database
        $query = "SELECT * FROM CATEGORIES ";
        //execute the SQL query and return records
        $result = mssql_query($query);
        //display the results 
        while($row = mssql_fetch_array($result))   
                {
                // Assign by reference
                $thisref = &amp;$refs[ $row['ID'] ];
                // add the the menu parent
                $thisref['IDCategoriePere'] = $row['IDCategoriePere'];
                $thisref['NOM'] = $row['NOM'];
                $thisref['URL'] = $row['URL'];
                // if there is no parent id
                if ($row['IDCategoriePere'] == 0)
                {
                    $list[ $row['ID'] ] = &amp;$thisref;
                }
                else
                {
                    $refs[ $row['IDCategoriePere'] ]['children'][ $row['ID'] ] = &amp;$thisref;
                }
        } 
        function create_menu( $arr )
            {
                $html = "\n&lt;ul&gt;\n";
                foreach ($arr as $key=&gt;$val) 
                {
                    $html .= '&lt;li&gt;&lt;a href="'.$val['URL'].'"&gt;'.$val['NOM']."&lt;/a&gt;&lt;/li&gt;\n";
                    if (array_key_exists('children', $val))
                    {
                        $html .= create_menu($val['children']);
                    }
                }
                $html .= "&lt;/ul&gt;\n";
                return $html;
            }
            echo create_menu( $list );
            //close the connection
         mssql_close($dbhandle);
         ?&gt; 
         &lt;/nav&gt;
&lt;/div&gt;

&lt;!-- WITHOUT PHP --&gt;
&lt;div id="container"&gt;
&lt;nav&gt;
    &lt;ul&gt;
        &lt;li&gt;&lt;a href="#"&gt;ACCUEIL&lt;/a&gt;&lt;/li&gt;

        &lt;li&gt;&lt;a href="#"&gt;PARCS&lt;/a&gt;
        &lt;!-- First Tier Drop Down --&gt;
        &lt;ul&gt;
            &lt;li&gt;&lt;a href="#"&gt;ALLEMAGNE&lt;/a&gt;
            &lt;!-- Second Tier Drop Down --&gt;
            &lt;ul&gt;
                &lt;li&gt;&lt;a href="#"&gt;BERLIN&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
            &lt;/li&gt;
            &lt;li&gt;&lt;a href="#"&gt;FRANCE&lt;/a&gt;
            &lt;/li&gt;
        &lt;/ul&gt;
        &lt;/li&gt;
        &lt;li&gt;&lt;a href="#"&gt;CONTACT&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
 &lt;/nav&gt;
&lt;/div&gt;
</code></pre>

<p><a href="https://i.stack.imgur.com/WkzNc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WkzNc.png" alt="enter image description here"></a></p>