How to Sort Data in Column (Ascending and Descending) in PHP/MySQL
Submitted by alpha_luna on Friday, June 24, 2016 - 13:18.
In this simple tutorial, we are going to learn on how to Sort Data in Column (Ascending and Descending) in PHP/MySQL. The user can learn on how to create a simple source code query in MySQL that can sort data by ascending to descending order.
Here's the default source code query.
Here's the ascending source code query.
Here's the descending source code query.
And, that's it. This is the steps on how to Sort Data in Column (Ascending and Descending) in PHP/MySQL.
Kindly click the "Download Code" button below for full source code. Thank you very much.
Hope that this tutorial will help you a lot.
Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.
Creating Database and Inserting Data
Our Database
- CREATE TABLE `member` (
- `member_id` INT(11) NOT NULL,
- `firstname` VARCHAR(100) NOT NULL,
- `lastname` VARCHAR(100) NOT NULL,
- `middlename` VARCHAR(100) NOT NULL,
- `address` VARCHAR(100) NOT NULL,
- `email` VARCHAR(100) NOT NULL
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Example Data
- INSERT INTO `member` (`member_id`, `firstname`, `lastname`, `middlename`, `address`, `email`) VALUES
Default Sorting Data
This sorting is default order as you can see in the image below.
- <div class="container">
- <br />
- <br />
- <div class="pull-right">
-
-
- </div>
- <br />
- <br />
- <br />
- <br />
- <table class="table table-bordered">
- <thead>
- <tr>
- </tr>
- </thead>
- <tbody>
- <?php
- $query=mysql_query("select * from member")or die(mysql_error());
- while($row=mysql_fetch_array($query)){
- $id=$row['member_id'];
- ?>
- <tr>
- </tr>
- <?php } ?>
- </tbody>
- </table>
- </div>
Ascending Sorting Data
This sorting is ascending order as you can see in the image below.
- <div class="container">
- <br />
- <br />
- <div class="pull-right">
-
-
- </div>
- <br />
- <br />
- <br />
- <br />
- <table class="table table-striped table-bordered">
- <thead>
- <tr>
- </tr>
- </thead>
- <tbody>
- <?php
- $query=mysql_query("select * from member order by firstname ASC")or die(mysql_error());
- while($row=mysql_fetch_array($query)){
- $id=$row['member_id'];
- ?>
- <tr>
- </tr>
- <?php } ?>
- </tbody>
- </table>
- </div>
Descending Sorting Data
This sorting is descending order as you can see in the image below.
- <div class="container">
- <br />
- <br />
- <div class="pull-right">
-
-
- </div>
- <br />
- <br />
- <br />
- <br />
- <table class="table table-striped table-bordered">
- <thead>
- <tr>
- </tr>
- </thead>
- <tbody>
- <?php
- $query=mysql_query("select * from member order by firstname DESC")or die(mysql_error());
- while($row=mysql_fetch_array($query)){
- $id=$row['member_id'];
- ?>
- <tr>
- </tr>
- <?php } ?>
- </tbody>
- </table>
- </div>
Add new comment
- 768 views