How to Sort Data in Column (Ascending and Descending) in PHP/MySQL

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.

Creating Database and Inserting Data

Our Database

  1. CREATE TABLE `member` (
  2.   `member_id` INT(11) NOT NULL,
  3.   `firstname` VARCHAR(100) NOT NULL,
  4.   `lastname` VARCHAR(100) NOT NULL,
  5.   `middlename` VARCHAR(100) NOT NULL,
  6.   `address` VARCHAR(100) NOT NULL,
  7.   `email` VARCHAR(100) NOT NULL
  8. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Example Data

  1. INSERT INTO `member` (`member_id`, `firstname`, `lastname`, `middlename`, `address`, `email`) VALUES
  2. (1, 'John', 'Meyer', 'Doe', 'United States', '[email protected]'),
  3. (2, 'Jane', 'Doe', 'Meyer', 'United States', '[email protected]'),
  4. (3, 'Andrea', 'Meyer', 'Doe', 'Mexico', '[email protected]'),
  5. (4, 'Rocky', 'Dew', 'Doe', 'Mexico', '[email protected]');

Default Sorting Data

This sorting is default order as you can see in the image below. Result Here's the default source code query.
  1. <div class="container">
  2. <br />
  3. <br />
  4. <div class="pull-right">
  5. <label style="font-weight:bold; color:blue; font-family:cursive; font-size:18px;">Order by:</label>
  6. <a class="btn" href="ascending.php">Ascending</a>
  7. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  8. <a class="btn" href="decending.php">Descending</a>
  9. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  10. <a class="btn btn-info" href="index.php">Default</a>
  11. </div>
  12. <br />
  13. <br />
  14. <br />
  15. <br />
  16. <table class="table table-bordered">
  17.         <thead>
  18.                 <tr>
  19.                         <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">FirstName</th>
  20.                         <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">LastName</th>
  21.                         <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">MiddleName</th>
  22.                         <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">Address</th>
  23.                         <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">Email</th>
  24.                 </tr>
  25.         </thead>
  26.         <tbody>
  27.                 <?php
  28.                 $query=mysql_query("select * from member")or die(mysql_error());
  29.                 while($row=mysql_fetch_array($query)){
  30.                 $id=$row['member_id'];
  31.                 ?>
  32.                 <tr>
  33.                         <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['firstname'] ?></td>
  34.                         <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['lastname'] ?></td>
  35.                         <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['middlename'] ?></td>
  36.                         <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['address'] ?></td>
  37.                         <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['email'] ?></td>
  38.                 </tr>
  39.                 <?php } ?>
  40.         </tbody>
  41. </div>

Ascending Sorting Data

This sorting is ascending order as you can see in the image below. Result Here's the ascending source code query.
  1. <div class="container">
  2. <br />
  3. <br />
  4. <div class="pull-right">
  5. <label style="font-weight:bold; color:blue; font-family:cursive; font-size:18px;">Order by:</label>
  6. <a class="btn btn-info" href="ascending.php">Ascending</a>
  7. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  8. <a class="btn btn-default" href="decending.php">Descending</a>
  9. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  10. <a class="btn btn-default" href="index.php">Default</a>
  11. </div>
  12. <br />
  13. <br />
  14. <br />
  15. <br />
  16. <table  class="table table-striped table-bordered">
  17.         <thead>
  18.                 <tr>
  19.                         <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">FirstName</th>
  20.                         <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">LastName</th>
  21.                         <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">MiddleName</th>
  22.                         <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">Address</th>
  23.                         <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">Email</th>
  24.                 </tr>
  25.         </thead>
  26.         <tbody>
  27.                 <?php
  28.                 $query=mysql_query("select * from member order by firstname ASC")or die(mysql_error());
  29.                 while($row=mysql_fetch_array($query)){
  30.                 $id=$row['member_id'];
  31.                 ?>                              
  32.                 <tr>                                                                   
  33.                         <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['firstname'] ?></td>
  34.                         <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['lastname'] ?></td>
  35.                         <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['middlename'] ?></td>
  36.                         <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['address'] ?></td>
  37.                         <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['email'] ?></td>
  38.                 </tr>
  39.                 <?php } ?>
  40.         </tbody>
  41. </div>

Descending Sorting Data

This sorting is descending order as you can see in the image below. Result Here's the descending source code query.
  1. <div class="container">
  2. <br />
  3. <br />
  4. <div class="pull-right">
  5. <label style="font-weight:bold; color:blue; font-family:cursive; font-size:18px;">Order by:</label>
  6. <a class="btn btn-default" href="ascending.php">Ascending</a>
  7. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  8. <a class="btn btn-info" href="decending.php">Descending</a>
  9. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  10. <a class="btn btn-default" href="index.php">Default</a>
  11. </div>
  12. <br />
  13. <br />
  14. <br />
  15. <br />
  16. <table  class="table table-striped table-bordered">
  17.         <thead>
  18.                 <tr>
  19.                         <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">FirstName</th>
  20.                         <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">LastName</th>
  21.                         <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">MiddleName</th>
  22.                         <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">Address</th>
  23.                         <th style="text-align:center; font-family:cursive; font-size:18px; color:blue;">Email</th>
  24.                 </tr>
  25.         </thead>
  26.         <tbody>
  27.                 <?php
  28.                 $query=mysql_query("select * from member order by firstname DESC")or die(mysql_error());
  29.                 while($row=mysql_fetch_array($query)){
  30.                 $id=$row['member_id'];
  31.                 ?>                              
  32.                 <tr>                                                                   
  33.                         <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['firstname'] ?></td>
  34.                         <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['lastname'] ?></td>
  35.                         <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['middlename'] ?></td>
  36.                         <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['address'] ?></td>
  37.                         <td style="text-align:center; font-family:cursive; font-size:18px;"><?php echo $row['email'] ?></td>
  38.                 </tr>
  39.                 <?php } ?>
  40.         </tbody>
  41. </div>
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.

Add new comment