Sorting Data in the Table with jQuery, PHP and MySQL

tableSorterPIIn this tutorial, I will teach you how to sort list of data in the table with jQuery, PHP and MySQL database.

This method provide primitives for sorting slices and user-defined collection of records. It is also efficient and quick for sorting of data in the table.

Let’s begin:

Sorting Data in the Table with jQuery, PHP and MySQL

  • Create a database in the MySQL and name it “dbsort
  • Make a query for creating table in the database that you have created.
CREATE TABLE IF NOT EXISTS `subject` (
 `SUBJ_ID` int(11) NOT NULL AUTO_INCREMENT,
 `SUBJ_CODE` varchar(30) NOT NULL,
 `SUBJ_DESCRIPTION` varchar(255) NOT NULL,
 `UNIT` int(2) NOT NULL,
 `YR` varchar(30) NOT NULL,
 `AY` varchar(30) NOT NULL,
 PRIMARY KEY (`SUBJ_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  • Make a query for inserting data in the table.
INSERT INTO `subject` (`SUBJ_ID`, `SUBJ_CODE`, `SUBJ_DESCRIPTION`, `UNIT`, `YR`, `AY`) VALUES
(11, 'Theology 1', 'Faith and Creed and Basic Catholic Doctrine', 3, 'Grade 7', '2013-2014'),
(12, 'English 0', 'English Plus ', 3, 'Grade 7', '2013-2014'),
(13, 'English 1', 'Developmental Reading', 3, 'Grade 7', '2013-2014'),
(14, 'Theology 1', 'Faith and Creed and Basic Catholic Doctrine', 3, 'Gade 8', '2013-2014'),
(15, 'History 1', 'Philippine History and Culture', 3, 'Grade 7', '2013-2014'),
(16, 'Psychology 1a', 'General Psychology with Moral Regen & Drug Abuse Ed.', 3, '22', '2013-2014'),
(17, 'Chem. 1', 'General and Organic Chemistry', 5, '22', '2013-2014'),
(18, 'Philosophy 1', 'Introduction to Philosophy', 3, '22', '2013-2014'),
(19, 'PE 1', 'Physical Education', 2, '22', '2013-2014'),
(20, 'NSTP 1', 'NSTP', 3, 'Grade 7', '2013-2014'),
(21, 'Theology 2', 'Bible and Salvation History ', 3, '22', '2013-2014'),
(22, 'English 1', 'Study and Thinking Skills ', 3, '22', '2013-2014'),
(23, 'English 02', 'Developmental Reading 2', 3, '22', '2013-2014');
  • Create a landing page and name it “index.php“.
  • Create a configuration between MySQL database and PHP script.
<?php
 $server = 'localhost';
 $dbuser = 'root';
 $dbpass = '';
 $dbname = 'quicksearchdb';
 $con = mysql_connect($server, $dbuser, $dbpass);
 if (isset($con)) {
 # code...
 $dbSelect = mysql_select_db($dbname);
 if (!$dbSelect) {
 echo "Problem in selecting database! Please contact administraator";
 die(mysql_error());
 }
 } else {
 echo "Problem in database connection! Please contact administraator";
 die(mysql_error());
 }
 ?>
  • Do the following codes for the layout, design and retrieving data in the database.
<h1>jQuery Table Sorter</h1> 
 <table id="dataTable" class="table table-bordered" style="font-size:11px;">
 <thead style="background-color:blue;color:white">
 <tr>
 <th width="10%">Subject Code</th>
 <th width="30%">Description</th>
 <th width="10%">Unit</th>
 <th width="10%">Year</th>
 <th width="10%">Academic Year</th> 
 </tr>
 </thead>
 <tbody>
 <?php 
 $sqlQuery = "SELECT * FROM `subject` limit 10";
 $result = mysql_query($sqlQuery) or die(mysql_error());

 while ($row = mysql_fetch_array($result)) {
 echo '<tr>'; 
 echo '<td>' .$row['SUBJ_CODE'].'</a></td>';
 echo '<td>'. $row['SUBJ_DESCRIPTION'].'</td>'; 
 echo '<td>'. $row['UNIT'].'</td>'; 
 echo '<td>'. $row['YR'].'</td>'; 
 echo '<td>'. $row['AY'].'</td>'; 
 echo '</tr>';
 }
 
 ?>
 </tbody>
 </table>
  • Set the following jQuery plugins.
 <script src="jquery/jquery-2.2.4.min.js"></script>
 <script src="jquery/tableSorter.jquery.js"></script>
  • Finally, create a script for sorting data  in the table.
<script>
 $(document).ready(function() { 
 $("#dataTable").tablesorter(); 
 } 
 ); 
 </script>

Download Sourcecode

Frequently Asked Questions

How does this PHP project work?

Built with vanilla PHP (no framework) and MySQL backend. Standard structure: form HTML, PHP script handlers, MySQL via PDO or mysqli, sessions for auth, Bootstrap for responsive layout. Ready to extend for BSIT capstone scope.

What PHP and MySQL versions does this project require?

Most projects in this batch run on PHP 7.4 to PHP 8.2 with MySQL 5.7+ or MariaDB 10+. A few older projects need PHP 5.6 (deprecated, use XAMPP 7.x). To run: install XAMPP / WAMP / Laragon, extract project to htdocs, import the included .sql file via phpMyAdmin, edit the connection file (usually config.php or db_connect.php) with your DB credentials, browse to the project URL in your browser.

How do I set up the database for this PHP project?

Open phpMyAdmin (http://localhost/phpmyadmin/ on XAMPP), create a new empty database with the name specified in the project’s config.php. Click the Import tab, choose the included .sql file, click Go. Then edit config.php (or includes/connection.php) with: ‘localhost’, your MySQL username (usually ‘root’), your MySQL password (usually blank for XAMPP), and the database name.

Can I use this PHP project for a BSIT capstone or thesis?

Yes, but extend it. A bare CRUD app is too narrow for full capstone scope. Add: user roles via session checks, reports/dashboards (Chart.js + AJAX), PDF exports (TCPDF library), email notifications (PHPMailer), real domain extension (analytics, audit log, multi-branch support). Pair with Chapter 1-5 documentation matching your panel’s rubric.

Why am I getting ‘connection error’ or ‘undefined function mysqli_connect’?

Three common PHP issues: (1) Connection error: Apache + MySQL services not running in XAMPP control panel, OR database name in config.php does not match what you created in phpMyAdmin. (2) ‘undefined function mysqli_connect’: MySQL extension not enabled, in php.ini uncomment extension=mysqli (then restart Apache). (3) ‘No such file or directory’: MySQL socket path wrong, use 127.0.0.1 instead of localhost in the connection string.

Where can I find more PHP projects with source code?

Browse the PHP Projects hub for the full library (300+ vanilla PHP systems). For modern PHP MVC alternatives see Laravel Projects (74 systems) or CodeIgniter Projects (32 systems). For BSIT-focused capstone idea lists see 150 Best Capstone Project Ideas.

Related PHP Projects

Leave a Comment