Skip to main content

Simple Dropdown Sort Using PHP


In this article we will create Simple Dropdown Sort using PHP. The program will dynamically sort the table row base on the selected options data. You are free to modify this, and use as you own it. To learn more about this tutorial, just follow the step below.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP 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_dropdown_sort 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_dropdown_sort");
  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">Simple Dropdown Sort Using PHP</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-8">
  18. <form method="POST" action="">
  19. <div class="form-inline">
  20. <label>Group:</label>
  21. <select class="form-control" name="group">
  22. <option value="Reptile">Reptile</option>
  23. <option value="Mammal">Mammal</option>
  24. </select>
  25. <button class="btn btn-primary" name="sort">Sort</button>
  26. <button class="btn btn-success" name="reset">Reset</button>
  27. </div>
  28. </form>
  29. <br /><br />
  30. <table class="table table-bordered">
  31. <thead class="alert-info">
  32. <th>Group</th>
  33. <th>Animal</th>
  34. </thead>
  35. <thead>
  36. <?php include'sort.php'?>
  37. </thead>
  38. </table>
  39. </div>
  40. </div>
  41. </body>
  42. </html>

Creating the Main Function

This code contains the main function of the application. The code will display a data base on the selected option To make this just copy and write these block of codes inside the text editor, then save it as sort.php

  1. <?php
  2. require'conn.php';
  3. if(ISSET($_POST['sort'])){
  4. $group=$_POST['group'];
  5.  
  6. $query=mysqli_query($conn, "SELECT * FROM `animals` WHERE `n_group`='$group'") or die(mysqli_error());
  7. while($fetch=mysqli_fetch_array($query)){
  8. echo"<tr><td>".$fetch['n_group']."</td><td>".$fetch['name']."</td></tr>";
  9. }
  10. }else if(ISSET($_POST['reset'])){
  11. $query=mysqli_query($conn, "SELECT * FROM `animals`") or die(mysqli_error());
  12. while($fetch=mysqli_fetch_array($query)){
  13. echo"<tr><td>".$fetch['n_group']."</td><td>".$fetch['name']."</td></tr>";
  14. }
  15. }else{
  16. $query=mysqli_query($conn, "SELECT * FROM `animals`") or die(mysqli_error());
  17. while($fetch=mysqli_fetch_array($query)){
  18. echo"<tr><td>".$fetch['n_group']."</td><td>".$fetch['name']."</td></tr>";
  19. }
  20. }
  21. ?>

There you have it we successfully created Simple Dropdown Sort 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.