PHP - Drop Down Filter Selection

Submitted by: 
Language: 
Visitors have accessed this post 394 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Drop Down Filter Selection</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-2"></div>
  19. <div class="col-md-8">
  20. <form method="POST" action="">
  21. <div class="form-inline">
  22. <label>Category:</label>
  23. <select class="form-control" name="category">
  24. <option value="Suzuki">Suzuki</option>
  25. <option value="Honda">Honda</option>
  26. <option value="Kawasaki">Kawasaki</option>
  27. </select>
  28. <button class="btn btn-primary" name="filter">Filter</button>
  29. <button class="btn btn-success" name="reset">Reset</button>
  30. </div>
  31. </form>
  32. <br /><br />
  33. <table class="table table-bordered">
  34. <thead class="alert-info">
  35. <th>Name</th>
  36. <th>Brand</th>
  37. </thead>
  38. <thead>
  39. <?php include'filter.php'?>
  40. </thead>
  41. </table>
  42. </div>
  43. </div>
  44. </body>
  45. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.