PHP - Simple Sort Column

Submitted by: 
Language: 
Visitors have accessed this post 300 times.

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 https://www.apachefriends.org/index.html.

And this is the link for the jquery that i used in this tutorial https://jquery.com/.

Lastly, 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_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_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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple Sort Column</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <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>
  19. <br /><br />
  20. <form method="POST" action="">
  21. <button class="btn btn-primary" name="asc"><span class="glyphicon glyphicon-arrow-up"></span> Ascending</button>
  22. <button class="btn btn-danger" name="desc"><span class="glyphicon glyphicon-arrow-down"></span> Descending</button>
  23. </form>
  24. <br /><br />
  25. <?php include 'sort.php'?>
  26. </div>
  27. <div class="modal fade" id="form_modal" aria-hidden="true">
  28. <div class="modal-dialog">
  29. <div class="modal-content">
  30. <form action="add.php" method="POST">
  31. <div class="modal-header">
  32. <h3 class="modal-title">Add Employee</h3>
  33. </div>
  34. <div class="modal-body">
  35. <div class="col-md-2"></div>
  36. <div class="col-md-8">
  37. <div class="form-group">
  38. <label>Firstname</label>
  39. <input type="text" name="firstname" class="form-control" required="required"/>
  40. </div>
  41. <div class="form-group">
  42. <label>Lastname</label>
  43. <input type="text" name="lastname" class="form-control" required="required"/>
  44. </div>
  45. <div class="form-group">
  46. <label>Position</label>
  47. <input type="text" name="position" class="form-control" required="required"/>
  48. </div>
  49. </div>
  50. </div>
  51. <br style="clear:both;"/>
  52. <div class="modal-footer">
  53. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  54. <button class="btn btn-primary" name="add" ><span class="glyphicon glyphicon-save"></span> Save</button>
  55. </div>
  56. </form>
  57. </div>
  58. </div>
  59. </div>
  60. <script src="js/jquery-3.2.1.min.js"></script>
  61. <script src="js/bootstrap.js"></script>
  62. </body>
  63. </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';
  3.  
  4. if(ISSET($_POST['add'])){
  5. $firstname = $_POST['firstname'];
  6. $lastname = $_POST['lastname'];
  7. $position = $_POST['position'];
  8.  
  9. mysqli_query($conn, "INSERT INTO `employee` VALUES('', '$firstname', '$lastname', '$position')") or die(mysqli_error());
  10. header("location: index.php");
  11. }
  12.  
  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';
  3.  
  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. ?>
  44.  
  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!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.