PHP - Sort Data Using PDO

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

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


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.