potinqy
State Management Engineer
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.
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.
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.
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.
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.
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
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.

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.
- <?php
- $conn
=
new
PDO(
'mysql:host=localhost;dbname=db_pdo_sort'
,
'root'
,
''
)
;
- if
(
!
$conn
)
{
- die
(
"Error: Failed to coonect to database!"
)
;
- }
- ?>
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.
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - Sort Data Using PDO</h3>
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-4">
- <form action="add.php" method="POST">
- <div class="form-group">
- <label>Firstname</label>
- <input type="text" name="firstname" class="form-control" required="required"/>
- </div>
- <div class="form-group">
- <label>Lastname</label>
- <input type="text" name="lastname" class="form-control" required="required"/>
- </div>
- <div class="form-group">
- <label>Address</label>
- <input type="text" name="address" class="form-control" required="required"/>
- </div>
- <center><button class="btn btn-primary" name="add" ><span class="glyphicon glyphicon-save"></span> Save</button></center>
- </form>
- </div>
- <div class="col-md-8">
- <form method="POST" action="">
- <button class="btn btn-primary" name="asc"><span class="glyphicon glyphicon-arrow-up"></span> Ascending</button>
- <button class="btn btn-danger" name="desc"><span class="glyphicon glyphicon-arrow-down"></span> Descending</button>
- </form>
- <br /><br />
- <?php
include
'sort.php'
?>
- </div>
- </div>
- </body>
- </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.
- <?php
- require_once
'conn.php'
;
- if
(
ISSET
(
$_POST
[
'add'
]
)
)
{
- $firstname
=
$_POST
[
'firstname'
]
;
- $lastname
=
$_POST
[
'lastname'
]
;
- $address
=
$_POST
[
'address'
]
;
- try{
- $conn
->
setAttribute
(
PDO::
ATTR_ERRMODE
,
PDO::
ERRMODE_EXCEPTION
)
;
- $sql
=
"INSERT INTO `member`(firstname, lastname, address) VALUES ('$firstname
', '$lastname
', '$address
')"
;
- $conn
->
exec
(
$sql
)
;
- }
catch(
PDOException $e
)
{
- echo
$e
->
getMessage
(
)
;
- }
- $conn
=
null
;
- header
(
"location: index.php"
)
;
- }
- ?>
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.
- <?php
- require
'conn.php'
;
- ?>
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- <th>Firstname</th>
- <th>Lastname</th>
- <th>Address</th>
- </tr>
- </thead>
- <tbody>
- <?php
- if
(
ISSET
(
$_POST
[
'asc'
]
)
)
{
- $query
=
$conn
->
prepare
(
"SELECT * FROM `member` ORDER BY `mem_id` ASC"
)
;
- $query
->
execute
(
)
;
- while
(
$row
=
$query
->
fetch
(
)
)
{
- echo
"<tr>
- <td>"
.
$row
[
'firstname'
]
.
"</td>
- <td>"
.
$row
[
'lastname'
]
.
"</td>
- <td>"
.
$row
[
'address'
]
.
"</td>
- </tr>"
;
- }
- }
else
if
(
ISSET
(
$_POST
[
'desc'
]
)
)
{
- $query
=
$conn
->
prepare
(
"SELECT * FROM `member` ORDER BY `mem_id` DESC"
)
;
- $query
->
execute
(
)
;
- while
(
$row
=
$query
->
fetch
(
)
)
{
- echo
"<tr>
- <td>"
.
$row
[
'firstname'
]
.
"</td>
- <td>"
.
$row
[
'lastname'
]
.
"</td>
- <td>"
.
$row
[
'address'
]
.
"</td>
- </tr>"
;
- }
- }
else
{
- $query
=
$conn
->
prepare
(
"SELECT * FROM `member`"
)
;
- $query
->
execute
(
)
;
- while
(
$row
=
$query
->
fetch
(
)
)
{
- echo
"<tr>
- <td>"
.
$row
[
'firstname'
]
.
"</td>
- <td>"
.
$row
[
'lastname'
]
.
"</td>
- <td>"
.
$row
[
'address'
]
.
"</td>
- </tr>"
;
- }
- }
- ?>
- </tbody>
- </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.