Three level mega menu in PHP and MySQL
<p>I have a parent child system. I am trying to create a three-level menu, but the first level menu keeps being repeated in all three levels.</p>

<p>Here is sample data of my database table</p>

<pre><code>id category                          parent
1  level a one                           0
2  level b one                           0
3  level two 1 for a                     1 
4  level two 2 for a                     1
5  level three for level two 1 for  a    3
</code></pre>

<p>PHP code:</p>

<pre><code>&lt;?php
    include("core/functions.php");
?&gt; 

&lt;div id="magik-verticalmenu" class="block magik-verticalmenu"&gt;
    &lt;div class="nav-title"&gt; &lt;span&gt;Categories&lt;/span&gt; &lt;/div&gt;
        &lt;div class="nav-content"&gt;
            &lt;div class="navbar navbar-inverse"&gt;
                &lt;div id="verticalmenu" class="verticalmenu" role="navigation"&gt;
&lt;?php 
$sql = "SELECT * FROM category WHERE parent = 0";
$pquery = $con-&gt;query($sql);      
?&gt;
                    &lt;div class="navbar"&gt;
                        &lt;div class="collapse navbar-collapse navbar-ex1-collapse"&gt;
                            &lt;ul class="nav navbar-nav verticalmenu"&gt;
&lt;?php 
while($parent = mysqli_fetch_assoc($pquery)):  
?&gt;                
&lt;?php 
    $parent_id = $parent['id'];
    $sql2 = "SELECT * FROM category WHERE parent = 'parent_id'";
    $cquery= $con-&gt;query($sql2); 
?&gt;   
    &lt;li class=" parent dropdown "&gt;
        &lt;a href="grid.html" class="dropdown-toggle" data-toggle="dropdown"&gt;
            &lt;span class="menu-title"&gt;
&lt;?php 
    echo $parent ['category'];
?&gt;
            &lt;/span&gt;&lt;b class="round-arrow"&gt;&lt;/b&gt;
        &lt;/a&gt;
        &lt;div class="dropdown-menu"&gt;
            &lt;div class="dropdown-menu-inner"&gt;
                &lt;div class="row"&gt;
                    &lt;div class="mega-col col-sm-66" data-widgets="wid-5" data-colwidth="6"&gt;
                        &lt;div class="mega-col-inner"&gt;
                            &lt;div class="ves-widget"&gt;
&lt;?php 
    while($child = mysqli_fetch_assoc($cquery)):
?&gt;
&lt;?php 
        $parent_id = $parent['id'];
        $sql3 = "SELECT * FROM category WHERE parent = 'parent_id'";
        $squery= $con-&gt;query($sql3); 
?&gt; 
                                &lt;div class="menu-title"&gt;
&lt;?php
        echo $child ['category'];
?&gt;
                                &lt;/div&gt;
&lt;?php
    endwhile;
?&gt;
                                &lt;div class="widget-html"&gt;
                                    &lt;div class="widget-inner"&gt;
                                        &lt;ul&gt;
&lt;?php 
    while($child = mysqli_fetch_assoc($squery)):
?&gt;     
                                            &lt;li class="first"&gt;
                                                &lt;a href="grid.html"&gt;
                                                    &lt;span&gt;
&lt;?php
        echo $child ['category'];
?&gt;
                                                    &lt;/span&gt;
                                                &lt;/a&gt;
                                            &lt;/li&gt;
&lt;?php
    endwhile;
?&gt;            
                                        &lt;/ul&gt;
                                    &lt;/div&gt;
                                &lt;/div&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/li&gt;
&lt;?php
endwhile;
?&gt;    
                            &lt;/ul&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
</code></pre>