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

PHP - Drop Down Filter Selection

gunnarbox

Crypto Whale Tracker
G Rep
0
0
0
Rep
0
G Vouches
0
0
0
Vouches
0
Posts
142
Likes
46
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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.
2019-09-09_22_51_44-localhost_127.0.0.1_db_selection_phpmyadmin_4.8.3.png

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. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Failed to connect to database!"

    )

    ;
  5. }
  6. ?>

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. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT * FROM `motors` WHERE `category`='$category

    '"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  6. while

    (

    $fetch

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  7. echo

    "<tr><td>"

    .

    $fetch

    [

    'name'

    ]

    .

    "</td><td>"

    .

    $fetch

    [

    'category'

    ]

    .

    "</td></tr>"

    ;
  8. }
  9. }

    else

    if

    (

    ISSET

    (

    $_POST

    [

    'reset'

    ]

    )

    )

    {
  10. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT * FROM `motors`"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  11. while

    (

    $fetch

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  12. echo

    "<tr><td>"

    .

    $fetch

    [

    'name'

    ]

    .

    "</td><td>"

    .

    $fetch

    [

    'category'

    ]

    .

    "</td></tr>"

    ;
  13. }
  14. }

    else

    {
  15. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT * FROM `motors`"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  16. while

    (

    $fetch

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  17. echo

    "<tr><td>"

    .

    $fetch

    [

    'name'

    ]

    .

    "</td><td>"

    .

    $fetch

    [

    'category'

    ]

    .

    "</td></tr>"

    ;
  18. }
  19. }
  20. ?>

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!


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

452,496

329,696

329,704

Top