Drop Down Filter Selection with MySQL in PHP

By CampCodes Administrator

Updated on:

drop down filter selection with mysql in php

Tutorial: Drop Down Filter Selection in PHP

In this tutorial, we will create a Drop Down Filter Selection with MySQL in PHP. This code will display a specific table row from MySQLi when the user selects a category from the drop-down list. The system uses the MySQLi SELECT query to show 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 for your system.

We will be using PHP as a scripting language and interpreter that is mainly used on any web server, including xamp, wamp, etc. It is being applied to many popular websites because of the modern approach as its today.

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the 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.

drop down selection in php

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.

<?php
    $conn=mysqli_connect("localhost", "root", "", "db_selection");
 
    if(!$conn){
        die("Error: Failed to connect to database!");
    }
?>

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.

READ ALSO:   User Registration using VueJs in PHP MySQL

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    </head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
        </div>
    </nav>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Drop Down Filter Selection</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <div class="col-md-2"></div>
        <div class="col-md-8">
            <form method="POST" action="">
                <div class="form-inline">
                    <label>Category:</label>
                    <select class="form-control" name="category">
                        <option value="Suzuki">Suzuki</option>
                        <option value="Honda">Honda</option>
                        <option value="Kawasaki">Kawasaki</option>
                    </select>
                    <button class="btn btn-primary" name="filter">Filter</button>
                    <button class="btn btn-success" name="reset">Reset</button>
                </div>
            </form>
            <br /><br />
            <table class="table table-bordered">
                <thead class="alert-info">
                    <th>Name</th>
                    <th>Brand</th>
                </thead>
                <thead>
                    <?php include'filter.php'?>
                </thead>
            </table>
        </div>
    </div>
</body>	
</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

<?php
    require'conn.php';
    if(ISSET($_POST['filter'])){
        $category=$_POST['category'];
 
        $query=mysqli_query($conn, "SELECT * FROM `motors` WHERE `category`='$category'") or die(mysqli_error());
        while($fetch=mysqli_fetch_array($query)){
            echo"<tr><td>".$fetch['name']."</td><td>".$fetch['category']."</td></tr>";
        }
    }else if(ISSET($_POST['reset'])){
        $query=mysqli_query($conn, "SELECT * FROM `motors`") or die(mysqli_error());
        while($fetch=mysqli_fetch_array($query)){
            echo"<tr><td>".$fetch['name']."</td><td>".$fetch['category']."</td></tr>";
        }
    }else{
        $query=mysqli_query($conn, "SELECT * FROM `motors`") or die(mysqli_error());
        while($fetch=mysqli_fetch_array($query)){
            echo"<tr><td>".$fetch['name']."</td><td>".$fetch['category']."</td></tr>";
        }
    }
?>

There you have it we successfully created Drop Down Filter Selection with MySQL in 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!

ajax multi select dropdown drop down filter selection with mysql in php drop down list in php form drop down list in php from a database dynamic drop down list in php from mysql dynamic drop down menu in php mysql edit drop down list in php mysql fetching data from mysql database to html dropdown list filter drop down list php filter in php demo how can i add multiple dropdown values in mysql using php? how can i fetch data after selecting from dropdown list? how can i select multiple options from a drop down list in php? how get value from dropdown in database in php? how to display multiple selected value of dropdownlist in php how to fetch data from database in dropdownlist in php how to filter data from a mysql database table with php how to get data from drop down list in php how to get data in dropdownlist from database in php how to get multiple selected values of select box in php how to get values in dropdown from database in php how to insert data into database from dropdownlist in php how to insert drop down list value in database in php how to insert multiple select dropdown in php how to insert multiple select values in database in php how to retrieve data from database whenever select in drop down list how to retrieve data from database whenever select in drop down list in php how to save dropdownlist selected value in database in php how to select multiple options from a drop down list in php how to select multiple values in dropdown in php how to show data in dropdownlist from database in php how to store multiple dropdown value in database how to store multiple select values in database using php html table with drop down filter list box in php multi-select dropdown filter in php with database multiple dependent drop down list in php multiple drop down filter php multiple drop down list in php using ajax multiple search filter in php multiple select box in php demo multiple select box in php example multiple select box in php mysql multiple select dropdown in php multiple select dropdown with checkbox in php multiple select php multiple selection in dropdown in php and mysql mysql filter results after query mysql filter results within search query php code for drop down list from database php drop down list php drop down list from mysql php drop down list mysql php drop down list selected value from database php drop down menu php drop down menu from database php filter table results php form drop down list mysql php get multiple select values post php mysql drop down menu to select data to display php mysql filter results drop down php retrieve data from mysql using drop down menu php select multiple array elements php select option php select option selected from database php tutorials populate a dropdown and display a related record in php search data from table in php select dropdown filter in php with database search select query in php with where clause selected value of dropdown in php from database selected value of dropdown in php mysql using php dropdown to filter mysql table data

Leave a Comment