PHP - Drop Down Filter Selection

In this tutorial we will create a Drop Down Filter Selection using PHP. This code will display a specific table row from MySQLi when user select a category from the drop down list. The code use MySQLi SELECT query to display a data in the MySQLi row by providing a "category" keyword in the WHERE clause category. This a user-friendly program feel free to modify and use it to your system. We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites because of modern approach as its today.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_selection, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn=mysqli_connect("localhost", "root", "", "db_selection");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Drop Down Filter Selection</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-2"></div>
  18. <div class="col-md-8">
  19. <form method="POST" action="">
  20. <div class="form-inline">
  21. <label>Category:</label>
  22. <select class="form-control" name="category">
  23. <option value="Suzuki">Suzuki</option>
  24. <option value="Honda">Honda</option>
  25. <option value="Kawasaki">Kawasaki</option>
  26. </select>
  27. <button class="btn btn-primary" name="filter">Filter</button>
  28. <button class="btn btn-success" name="reset">Reset</button>
  29. </div>
  30. </form>
  31. <br /><br />
  32. <table class="table table-bordered">
  33. <thead class="alert-info">
  34. <th>Name</th>
  35. <th>Brand</th>
  36. </thead>
  37. <thead>
  38. <?php include'filter.php'?>
  39. </thead>
  40. </table>
  41. </div>
  42. </div>
  43. </body>
  44. </html>

Creating the Main Function

This code contains the main function of the application. This code will display a specific table row base on category when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as filter.php
  1. <?php
  2. require'conn.php';
  3. if(ISSET($_POST['filter'])){
  4. $category=$_POST['category'];
  5.  
  6. $query=mysqli_query($conn, "SELECT * FROM `motors` WHERE `category`='$category'") or die(mysqli_error());
  7. while($fetch=mysqli_fetch_array($query)){
  8. echo"<tr><td>".$fetch['name']."</td><td>".$fetch['category']."</td></tr>";
  9. }
  10. }else if(ISSET($_POST['reset'])){
  11. $query=mysqli_query($conn, "SELECT * FROM `motors`") or die(mysqli_error());
  12. while($fetch=mysqli_fetch_array($query)){
  13. echo"<tr><td>".$fetch['name']."</td><td>".$fetch['category']."</td></tr>";
  14. }
  15. }else{
  16. $query=mysqli_query($conn, "SELECT * FROM `motors`") or die(mysqli_error());
  17. while($fetch=mysqli_fetch_array($query)){
  18. echo"<tr><td>".$fetch['name']."</td><td>".$fetch['category']."</td></tr>";
  19. }
  20. }
  21. ?>
There you have it we successfully created Drop Down Filter Selection using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment