How to Create a Dynamic Dropdown Menu using PHP/MySQLi and Bootstrap 4

Getting Started

First, we need the jQuery library and Bootstrap 4. I've included these files in the downloadable of this tutorial but if you want, you can download them yourself using the links below: For jQuery For Bootstrap 4 Note: Bootstrap JS depends on jQuery to work. Therefore you need to declare jQuery first before declaring boostrap js.

Creating our Database

Next, we create the database that we are going to filter in this tutorial. I've included a SQL file in the downloadable of this tutorial. All you have to do is import the said file. If you have no idea on how to import, please visit my tutorial How import .sql file to restore MySQL database. You should be able to create a database named test.

Displaying our Nav

Lastly, we display our nav with the dynamic dropdown list from our database. Create a new file, name it as index.php and paste the codes below.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.         <meta charset="utf-8">
  5.         <title>How to Create a Dynamic Dropdown Menu using PHP/MySQLi and Bootstrap 4</title>
  6.         <link rel="stylesheet" type="text/css" href="bootstrap4/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <nav class="navbar navbar-expand-lg navbar-light bg-light">
  10.         <a class="navbar-brand" href="#">SourceCodeSter</a>
  11.         <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
  12.         <span class="navbar-toggler-icon"></span>
  13.         </button>
  14.  
  15.         <div class="collapse navbar-collapse" id="navbarSupportedContent">
  16.         <ul class="navbar-nav mr-auto">
  17.                 <li class="nav-item dropdown">
  18.                         <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  19.                         Category
  20.                         </a>
  21.                         <div class="dropdown-menu" aria-labelledby="navbarDropdown">
  22.                                 <?php
  23.                                         //connection
  24.                                         $conn = new mysqli('localhost', 'root', '', 'test');
  25.  
  26.                                         $sql = "SELECT * FROM categories";
  27.                                         $query = $conn->query($sql);
  28.  
  29.                                         while($row = $query->fetch_assoc()){
  30.                                                 echo "
  31.                                                         <a class='dropdown-item' href='#'>".$row['name']."</a>
  32.                                                 ";
  33.                                         }
  34.                                 ?>
  35.                         </div>
  36.                 </li>
  37.         </ul>
  38.         </div>
  39. </nav>
  40.  
  41. <script src="jquery.min.js"></script>
  42. <script src="bootstrap4/js/bootstrap.min.js"></script>
  43. </body>
  44. </html>
That ends this tutorial. Happy Coding :)

Add new comment