• We just launched and are currently in beta. Join us as we build and grow the community.

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

zorako

Absurdity Curator
Z Rep
0
0
0
Rep
0
Z Vouches
0
0
0
Vouches
0
Posts
78
Likes
181
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. <div class="collapse navbar-collapse" id="navbarSupportedContent">
  15. <ul class="navbar-nav mr-auto">
  16. <li class="nav-item dropdown">
  17. <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  18. Category
  19. </a>
  20. <div class="dropdown-menu" aria-labelledby="navbarDropdown">
  21. <?php
  22. //connection
  23. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'test'

    )

    ;

  24. $sql

    =

    "SELECT * FROM categories"

    ;
  25. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  26. while

    (

    $row

    =

    $query

    ->

    fetch_assoc

    (

    )

    )

    {
  27. echo

    "
  28. <a class='dropdown-item' href='#'>"

    .

    $row

    [

    'name'

    ]

    .

    "</a>
  29. "

    ;
  30. }
  31. ?>
  32. </div>
  33. </li>
  34. </ul>
  35. </div>
  36. </nav>

  37. <script src="jquery.min.js"></script>
  38. <script src="bootstrap4/js/bootstrap.min.js"></script>
  39. </body>
  40. </html>

That ends this tutorial. Happy Coding :)


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,292

324,186

324,194

Top