Skip to main content

PHP - Sort Data Using PDO


In this tutorial we will create a Sort Data using PDO. This code can sort a data in the database server with the use of PDO query when user click a buttin. The code use a PDO query to display and sort a specific data in the table row by adding a ORDER BY parameter with a keyword and applying a condition ASCENDING or DESCENDING.

We will be using PDO as a query scripting it an acronym for PHP Data Objects. It is a lean, clean, and consistent way to access databases. This means developers can write portable code much easier.

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 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_pdo_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 = new PDO( 'mysql:host=localhost;dbname=db_pdo_sort', 'root', '');
  3. if(!$conn){
  4. die("Error: Failed to coonect to database!");
  5. }
  6. ?>

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">PHP - Sort Data Using PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  18. <form action="add.php" method="POST">
  19. <div class="form-group">
  20. <label>Firstname</label>
  21. <input type="text" name="firstname" class="form-control" required="required"/>
  22. </div>
  23. <div class="form-group">
  24. <label>Lastname</label>
  25. <input type="text" name="lastname" class="form-control" required="required"/>
  26. </div>
  27. <div class="form-group">
  28. <label>Address</label>
  29. <input type="text" name="address" class="form-control" required="required"/>
  30. </div>
  31.  
  32. <center><button class="btn btn-primary" name="add" ><span class="glyphicon glyphicon-save"></span> Save</button></center>
  33. </form>
  34. </div>
  35. <div class="col-md-8">
  36. <form method="POST" action="">
  37. <button class="btn btn-primary" name="asc"><span class="glyphicon glyphicon-arrow-up"></span> Ascending</button>
  38. <button class="btn btn-danger" name="desc"><span class="glyphicon glyphicon-arrow-down"></span> Descending</button>
  39. </form>
  40. <br /><br />
  41. <?php include 'sort.php'?>
  42. </div>
  43. </div>
  44. </body>
  45. </html>

Creating the PDO Query

This code contains the pdo query of the application. This code will store the form inputs to the MySQLi database server. To make this just copy and write these block of codes below inside the text editor, then save it as add.php.

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['add'])){
  5.  
  6. $firstname = $_POST['firstname'];
  7. $lastname = $_POST['lastname'];
  8. $address = $_POST['address'];
  9.  
  10.  
  11. try{
  12. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  13. $sql = "INSERT INTO `member`(firstname, lastname, address) VALUES ('$firstname', '$lastname', '$address')";
  14. $conn->exec($sql);
  15. }catch(PDOException $e){
  16. echo $e->getMessage();
  17. }
  18.  
  19. $conn = null;
  20.  
  21. header("location: index.php");
  22.  
  23. }
  24. ?>

Creating the Main Function

This code contains the main function of the application. This code will sort a data when the button is clicked. To do this just kindly write these block of codes 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>Address</th>
  11. </tr>
  12. </thead>
  13. <tbody>
  14.  
  15. <?php
  16. if(ISSET($_POST['asc'])){
  17. $query = $conn->prepare("SELECT * FROM `member` ORDER BY `mem_id` ASC");
  18. $query->execute();
  19. while($row = $query->fetch()){
  20. echo "<tr>
  21. <td>".$row['firstname']."</td>
  22. <td>".$row['lastname']."</td>
  23. <td>".$row['address']."</td>
  24. </tr>";
  25. }
  26. }else if(ISSET($_POST['desc'])){
  27. $query = $conn->prepare("SELECT * FROM `member` ORDER BY `mem_id` DESC");
  28. $query->execute();
  29. while($row = $query->fetch()){
  30. echo "<tr>
  31. <td>".$row['firstname']."</td>
  32. <td>".$row['lastname']."</td>
  33. <td>".$row['address']."</td>
  34. </tr>";
  35. }
  36. }else{
  37. $query = $conn->prepare("SELECT * FROM `member`");
  38. $query->execute();
  39. while($row = $query->fetch()){
  40. echo "<tr>
  41. <td>".$row['firstname']."</td>
  42. <td>".$row['lastname']."</td>
  43. <td>".$row['address']."</td>
  44. </tr>";
  45. }
  46. }
  47. ?>
  48.  
  49. </tbody>
  50. </table>

There you have it we successfully created a Sort Data using PDO. 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.