Skip to main content

Simple Dropdown Sort Using PHP

Screenshot
Body

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. <br />
  2. <?php<br />
  3. $conn=mysqli_connect("localhost", "root", "", "db_dropdown_sort");</p>
  4. <p> if(!$conn){<br />
  5. die("Error: Failed to connect to database!");<br />
  6. }<br />
  7. ?><br />

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. <br />
  2. <!DOCTYPE html><br />
  3. <html lang="en"><br />
  4. <head><br />
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/><br />
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/><br />
  7. </head><br />
  8. <body><br />
  9. <nav class="navbar navbar-default"><br />
  10. <div class="container-fluid"><br />
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a><br />
  12. </div><br />
  13. </nav><br />
  14. <div class="col-md-3"></div><br />
  15. <div class="col-md-6 well"><br />
  16. <h3 class="text-primary">Simple Dropdown Sort Using PHP</h3><br />
  17. <hr style="border-top:1px dotted #ccc;"/><br />
  18. <div class="col-md-8"><br />
  19. <form method="POST" action=""><br />
  20. <div class="form-inline"><br />
  21. <label>Group:</label><br />
  22. <select class="form-control" name="group"><br />
  23. <option value="Reptile">Reptile</option><br />
  24. <option value="Mammal">Mammal</option><br />
  25. </select><br />
  26. <button class="btn btn-primary" name="sort">Sort</button><br />
  27. <button class="btn btn-success" name="reset">Reset</button><br />
  28. </div><br />
  29. </form><br />
  30. <br /><br /><br />
  31. <table class="table table-bordered"><br />
  32. <thead class="alert-info"><br />
  33. <th>Group</th><br />
  34. <th>Animal</th><br />
  35. </thead><br />
  36. <thead><br />
  37. <?php include'sort.php'?><br />
  38. </thead><br />
  39. </table><br />
  40. </div><br />
  41. </div><br />
  42. </body><br />
  43. </html><br />

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

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!

Add new comment