title: How to fetch menu and sub-menu from database using nested do while loop?
body: <p>I kindly would like someone to help me on php code. What I want to do is to fetch Menu  and Sub-menu from database using nested do while loop from two tables so  it shows a drop-down navigation bar. Navigation table holds menu items and category table holds sub-menu. But it stops on first iteration of the loop after it fetch all menu items and the first sub-menu items for the first menu item then it shows nothing but empty result. </p>
  
  <pre><code>-- Table structure for table `category`
  --
  
  CREATE TABLE `category` (
    `cat_id` int(11) NOT NULL,
    `nav_id` int(11) NOT NULL,
    `cat_eng` varchar(256) NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  
  --
  -- Dumping data for table `category`
  --
  
  INSERT INTO `category` (`cat_id`, `nav_id`, `cat_eng`) VALUES
  (1, 1, 'Technology'),
  (2, 1, 'Science'),
  (3, 1, 'Mathemathics'),
  (4, 1, 'Computer'),
  (5, 2, 'Geography'),
  (6, 2, 'Environment'),
  (7, 2, 'Weather'),
  (8, 2, 'World'),
  (9, 3, 'Sport'),
  (10, 3, 'Food'),
  (11, 3, 'Health'),
  (12, 4, 'Mens'),
  (13, 4, 'Womens');
  
  -- --------------------------------------------------------
  
  --
  -- Table structure for table `navigation`
  --
  
  CREATE TABLE `navigation` (
    `nav_id` int(11) NOT NULL,
    `nav_eng` varchar(256) NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  
  --
  -- Dumping data for table `navigation`
  --
  
  INSERT INTO `navigation` (`nav_id`, `nav_eng`) VALUES
  (1, 'Educational'),
  (2, 'General-Knowlege'),
  (3, 'Life-style'),
  (4, 'Fashion');
  </code></pre>
  
  <p>Connection.php</p>
  
  <pre><code>&lt;?php 
    session_start();
  
    $host = 'localhost';
    $user = 'root';
    $password = '';
    $dbname = 'test';
  
    $connection = @mysqli_connect($host, $user, $password, $dbname);
  
    if (!$connection) {
      die("Connection failed: " . mysqli_connect_error());
    }
  
    mysqli_set_charset($connection, 'utf8');
   ?&gt;
  
  &lt;div class="collapse navbar-collapse" id="navbarSupportedContent"&gt;
            &lt;ul class="navbar-nav mr-auto"&gt;
  
              &lt;!-- FETCHING NAVIGATION LIST ALONG WITH DROPDOWN MENU FROM DATABASE  --&gt;
              &lt;?php
                  //SETTING GET PAGES
                  if (isset($_GET['nav'])) {
                      $pageid = $_GET['nav'];
                  }else {
                      $pageid = 1;
                  }
  
                  //NAVIGATION TABLE QUERY
                   $nav_sql = "SELECT * FROM navigation";
                   $nav_query = mysqli_query($connection, $nav_sql);
                   $nav_result = mysqli_fetch_assoc($nav_query);
  
                   //CATEGORY TABLE QUERY
                    $cat_sql = "SELECT category.*, navigation.nav_id AS id FROM category JOIN navigation ON (category.nav_id = navigation.nav_id) WHERE category.nav_id = " .$pageid;
                    $cat_query = mysqli_query($connection, $cat_sql);
                    $cat_result = mysqli_fetch_assoc($cat_query);
                  do {
                    ?&gt; 
                    &lt;li class="nav-item dropdown"&gt;
                      &lt;a class="nav-link dropdown-toggle" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" href="?nav=&lt;?php echo $nav_result['nav_id'] ?&gt;"&gt; &lt;?php echo $nav_result['nav_eng']; ?&gt;&lt;/a&gt;
                      &lt;ul class="dropdown-menu"&gt;
                        &lt;?php 
                          do { ?&gt;
                            &lt;li&gt;
                              &lt;a class="dropdown-item" href="?nav=&lt;?php echo $cat_result['cat_id']; ?&gt;"&gt;&lt;?php echo $cat_result['cat_eng'] ?&gt;&lt;/a&gt;
                            &lt;/li&gt; &lt;?php 
                          } while ($cat_result = mysqli_fetch_assoc($cat_query));
                         ?&gt;
                      &lt;/ul&gt;
                    &lt;/li&gt; &lt;?php
                  } while ($nav_result = mysqli_fetch_assoc($nav_query));
  
               ?&gt;
            &lt;/ul&gt;
           &lt;/div&gt;
  </code></pre>