Skip to main content

PHP - Simple Sort Column

In this tutorial we will create a Simple Sort Column using PHP. This code will arrange the table data in a sortable way by changing to ascending to descending. This code use MySQLi ORDER BY by adding a parameter ASC or DESC to change the arrangement of data in the table list.This is a user-friendly kind of program feel free to modify it.

We will be using PHP as a scripting language that manage a database server to handle a bulk of data per transaction. It describe as an advance technology that manage both server and control-block of your machine.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server

And this is the link for the jquery that i used in this tutorial

Lastly, this is the link for the bootstrap that i used for the layout design

Creating Database

Open your database web server then create a database name in it db_sort, after that click Import then locate the database file inside the folder of the application then click ok.

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_sort");
  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="">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 - Simple Sort Column</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button type="button" class="btn btn-success pull-right" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add employee</button>
  18. <br /><br />
  19. <form method="POST" action="">
  20. <button class="btn btn-primary" name="asc"><span class="glyphicon glyphicon-arrow-up"></span> Ascending</button>
  21. <button class="btn btn-danger" name="desc"><span class="glyphicon glyphicon-arrow-down"></span> Descending</button>
  22. </form>
  23. <br /><br />
  24. <?php include 'sort.php'?>
  25. </div>
  26. <div class="modal fade" id="form_modal" aria-hidden="true">
  27. <div class="modal-dialog">
  28. <div class="modal-content">
  29. <form action="add.php" method="POST">
  30. <div class="modal-header">
  31. <h3 class="modal-title">Add Employee</h3>
  32. </div>
  33. <div class="modal-body">
  34. <div class="col-md-2"></div>
  35. <div class="col-md-8">
  36. <div class="form-group">
  37. <label>Firstname</label>
  38. <input type="text" name="firstname" class="form-control" required="required"/>
  39. </div>
  40. <div class="form-group">
  41. <label>Lastname</label>
  42. <input type="text" name="lastname" class="form-control" required="required"/>
  43. </div>
  44. <div class="form-group">
  45. <label>Position</label>
  46. <input type="text" name="position" class="form-control" required="required"/>
  47. </div>
  48. </div>
  49. </div>
  50. <br style="clear:both;"/>
  51. <div class="modal-footer">
  52. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  53. <button class="btn btn-primary" name="add" ><span class="glyphicon glyphicon-save"></span> Save</button>
  54. </div>
  55. </form>
  56. </div>
  57. </div>
  58. </div>
  59. <script src="js/jquery-3.2.1.min.js"></script>
  60. <script src="js/bootstrap.js"></script>
  61. </body>
  62. </html>

Creating PHP Query

This code contains the php query of the application. This code will save the employee data inputs to the MySQLi database server. To do that just copy and write this block of codes inside the text editor, then save it as add.php.

  1. <?php
  2. require_once 'conn.php';
  4. if(ISSET($_POST['add'])){
  5. $firstname = $_POST['firstname'];
  6. $lastname = $_POST['lastname'];
  7. $position = $_POST['position'];
  9. mysqli_query($conn, "INSERT INTO `employee` VALUES('', '$firstname', '$lastname', '$position')") or die(mysqli_error());
  10. header("location: index.php");
  11. }
  13. ?>

Creating the Main Function

This code contains the main function of the application. This code will sort the data in the table 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 sort.php

  1. <?php
  2. require 'conn.php';
  4. ?>
  5. <table class="table table-bordered">
  6. <thead class="alert-info">
  7. <tr>
  8. <th>Firstname</th>
  9. <th>Lastname</th>
  10. <th>Position</th>
  11. </tr>
  12. </thead>
  13. <tbody>
  14. <?php
  15. if(ISSET($_POST['asc'])){
  16. $query = mysqli_query($conn, "SELECT * FROM `employee` ORDER BY `lastname` ASC") or die(mysqli_error());
  17. while($fetch = mysqli_fetch_array($query)){
  18. echo "<tr>
  19. <td>".$fetch['firstname']."</td>
  20. <td>".$fetch['lastname']."</td>
  21. <td>".$fetch['position']."</td>
  22. </tr>";
  23. }
  24. }else if(ISSET($_POST['desc'])){
  25. $query = mysqli_query($conn, "SELECT * FROM `employee` ORDER BY `lastname` DESC") or die(mysqli_error());
  26. while($fetch = mysqli_fetch_array($query)){
  27. echo "<tr>
  28. <td>".$fetch['firstname']."</td>
  29. <td>".$fetch['lastname']."</td>
  30. <td>".$fetch['position']."</td>
  31. </tr>";
  32. }
  33. }else{
  34. $query = mysqli_query($conn, "SELECT * FROM `employee`") or die(mysqli_error());
  35. while($fetch = mysqli_fetch_array($query)){
  36. echo "<tr>
  37. <td>".$fetch['firstname']."</td>
  38. <td>".$fetch['lastname']."</td>
  39. <td>".$fetch['position']."</td>
  40. </tr>";
  41. }
  42. }
  43. ?>
  45. </tbody>
  46. </table>

There you have it we successfully created Simple Sort Column 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 for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


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

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