• We just launched and are currently in beta. Join us as we build and grow the community.

PHP - Sort Data Using PDO

potinqy

State Management Engineer
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
153
Likes
117
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
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.
2020-01-09_21_43_31-localhost_127.0.0.1_db_pdo_sort_phpmyadmin_4.8.3.png

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. <center><button class="btn btn-primary" name="add" ><span class="glyphicon glyphicon-save"></span> Save</button></center>
  32. </form>
  33. </div>
  34. <div class="col-md-8">
  35. <form method="POST" action="">
  36. <button class="btn btn-primary" name="asc"><span class="glyphicon glyphicon-arrow-up"></span> Ascending</button>
  37. <button class="btn btn-danger" name="desc"><span class="glyphicon glyphicon-arrow-down"></span> Descending</button>
  38. </form>
  39. <br /><br />
  40. <?php

    include

    'sort.php'

    ?>
  41. </div>
  42. </div>
  43. </body>
  44. </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. if

    (

    ISSET

    (

    $_POST

    [

    'add'

    ]

    )

    )

    {

  4. $firstname

    =

    $_POST

    [

    'firstname'

    ]

    ;
  5. $lastname

    =

    $_POST

    [

    'lastname'

    ]

    ;
  6. $address

    =

    $_POST

    [

    'address'

    ]

    ;


  7. try{
  8. $conn

    ->

    setAttribute

    (

    PDO::

    ATTR_ERRMODE

    ,

    PDO::

    ERRMODE_EXCEPTION

    )

    ;
  9. $sql

    =

    "INSERT INTO `member`(firstname, lastname, address) VALUES ('$firstname

    ', '$lastname

    ', '$address

    ')"

    ;
  10. $conn

    ->

    exec

    (

    $sql

    )

    ;
  11. }

    catch(

    PDOException $e

    )

    {
  12. echo

    $e

    ->

    getMessage

    (

    )

    ;
  13. }

  14. $conn

    =

    null

    ;

  15. header

    (

    "location: index.php"

    )

    ;

  16. }
  17. ?>

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. <table class="table table-bordered">
  5. <thead class="alert-info">
  6. <tr>
  7. <th>Firstname</th>
  8. <th>Lastname</th>
  9. <th>Address</th>
  10. </tr>
  11. </thead>
  12. <tbody>

  13. <?php
  14. if

    (

    ISSET

    (

    $_POST

    [

    'asc'

    ]

    )

    )

    {
  15. $query

    =

    $conn

    ->

    prepare

    (

    "SELECT * FROM `member` ORDER BY `mem_id` ASC"

    )

    ;
  16. $query

    ->

    execute

    (

    )

    ;
  17. while

    (

    $row

    =

    $query

    ->

    fetch

    (

    )

    )

    {
  18. echo

    "<tr>
  19. <td>"

    .

    $row

    [

    'firstname'

    ]

    .

    "</td>
  20. <td>"

    .

    $row

    [

    'lastname'

    ]

    .

    "</td>
  21. <td>"

    .

    $row

    [

    'address'

    ]

    .

    "</td>
  22. </tr>"

    ;
  23. }
  24. }

    else

    if

    (

    ISSET

    (

    $_POST

    [

    'desc'

    ]

    )

    )

    {
  25. $query

    =

    $conn

    ->

    prepare

    (

    "SELECT * FROM `member` ORDER BY `mem_id` DESC"

    )

    ;
  26. $query

    ->

    execute

    (

    )

    ;
  27. while

    (

    $row

    =

    $query

    ->

    fetch

    (

    )

    )

    {
  28. echo

    "<tr>
  29. <td>"

    .

    $row

    [

    'firstname'

    ]

    .

    "</td>
  30. <td>"

    .

    $row

    [

    'lastname'

    ]

    .

    "</td>
  31. <td>"

    .

    $row

    [

    'address'

    ]

    .

    "</td>
  32. </tr>"

    ;
  33. }
  34. }

    else

    {
  35. $query

    =

    $conn

    ->

    prepare

    (

    "SELECT * FROM `member`"

    )

    ;
  36. $query

    ->

    execute

    (

    )

    ;
  37. while

    (

    $row

    =

    $query

    ->

    fetch

    (

    )

    )

    {
  38. echo

    "<tr>
  39. <td>"

    .

    $row

    [

    'firstname'

    ]

    .

    "</td>
  40. <td>"

    .

    $row

    [

    'lastname'

    ]

    .

    "</td>
  41. <td>"

    .

    $row

    [

    'address'

    ]

    .

    "</td>
  42. </tr>"

    ;
  43. }
  44. }
  45. ?>

  46. </tbody>
  47. </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!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,695

323,213

323,222

Top