ItSolutionStuff.com

PHP MySQL Column Sorting Example Tutorial

By Hardik Savani • May 14, 2024
PHP MySql

In this example, i will show you simple way to create table column sorting from database using php mysql. we will do it sorting column by clicking column header with php and mysql in table.

It is a very basic example for php starter to create column sort ascending descending order by click on column heading. It is very simple way to create without using ajax. so we can create this example in a single file.

in this example we will create "countries" with name and code column. Then after will create one index file and manage column sorting. You can simply see below preview of this example.

Preview:

Create countries table:

Here, we will create countries table by following sql query:

CREATE TABLE IF NOT EXISTS `countries` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(80) NOT NULL,

`code` varchar(244) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

Create index.php file:

<!DOCTYPE html>

<html>

<head>

<title>Column Sorting using PHP and MySQL - ItSolutionStuff.com</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />

</head>

<body>

<div class="container">

<h1>Column Sorting using PHP and MySQL - ItSolutionStuff.com</h1>

<?php

$hostName = "localhost";

$username = "root";

$password = "root";

$dbname = "test";

$mysqli = new mysqli($hostName, $username, $password, $dbname);

$orderBy = !empty($_GET["orderby"]) ? $_GET["orderby"] : "name";

$order = !empty($_GET["order"]) ? $_GET["order"] : "asc";

$sql = "SELECT * FROM countries ORDER BY " . $orderBy . " " . $order;

$result = $mysqli->query($sql);

$nameOrder = "asc";

$codeOrder = "asc";

if($orderBy == "name" && $order == "asc") {

$nameOrder = "desc";

}

if($orderBy == "code" && $order == "asc") {

$codeOrder = "desc";

}

?>

<table class="table table-bordered">

<thead>

<tr>

<th><a href="?orderby=name&order=<?php echo $nameOrder; ?>">Name</a></th>

<th><a href="?orderby=code&order=<?php echo $codeOrder; ?>">Code</a></th>

</tr>

</thead>

<tbody>

<?php

while($row = mysqli_fetch_assoc($result)){

?>

<tr>

<td><?php echo $row['name']; ?></td>

<td><?php echo $row['code']; ?></td>

</tr>

<?php

}

?>

</tbody>

</table>

</div>

</body>

</html>

Now you can simply run.

I hope it can help you....

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Column Sorting with Pagination Example

Read Now →

PHP MySQL DataTables Server-side Processing Example

Read Now →

PHP Ajax Infinite Scroll Pagination Example

Read Now →

PHP Bootstrap Autocomplete Tokenfield using Ajax Example

Read Now →

Multiple File Upload using Dropzone JS in PHP Example

Read Now →

PHP CRUD Operation Using Ajax and JQuery Example

Read Now →

Convert HTML to PDF in PHP with Dompdf Example

Read Now →

PHP Capture Screenshot of Website from URL Example

Read Now →

PHP JQuery Select2 Ajax Autocomplete Example

Read Now →

AngularJS Sorting(using Orderby Filter) Table Data Example

Read Now →

JQuery Draggable Sortable Table Rows Example

Read Now →

PHP Paypal Payment Gateway Integration Example

Read Now →

How to Get a Current Page URL in PHP?

Read Now →