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.
-- 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');
Connection.php
<?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');
?>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<!-- FETCHING NAVIGATION LIST ALONG WITH DROPDOWN MENU FROM DATABASE -->
<?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 {
?>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" href="?nav=<?php echo $nav_result['nav_id'] ?>"> <?php echo $nav_result['nav_eng']; ?></a>
<ul class="dropdown-menu">
<?php
do { ?>
<li>
<a class="dropdown-item" href="?nav=<?php echo $cat_result['cat_id']; ?>"><?php echo $cat_result['cat_eng'] ?></a>
</li> <?php
} while ($cat_result = mysqli_fetch_assoc($cat_query));
?>
</ul>
</li> <?php
} while ($nav_result = mysqli_fetch_assoc($nav_query));
?>
</ul>
</div>