How to sort rows of HTML table that are called from MySQL
The easiest way to do this would be to put a link on your column headers, pointing to the same page. In the query string, put a variable so that you know what they clicked on, and then use ORDER BY in your SQL query to perform the ordering.
The HTML would look like this:
<th><a href="mypage.php?sort=type">Type:</a></th><th><a href="mypage.php?sort=desc">Description:</a></th><th><a href="mypage.php?sort=recorded">Recorded Date:</a></th><th><a href="mypage.php?sort=added">Added Date:</a></th>
And in the php code, do something like this:
<?php$sql = "SELECT * FROM MyTable";if ($_GET['sort'] == 'type'){ $sql .= " ORDER BY type";}elseif ($_GET['sort'] == 'desc'){ $sql .= " ORDER BY Description";}elseif ($_GET['sort'] == 'recorded'){ $sql .= " ORDER BY DateRecorded";}elseif($_GET['sort'] == 'added'){ $sql .= " ORDER BY DateAdded";}$>
Notice that you shouldn't take the $_GET value directly and append it to your query. As some user could got to MyPage.php?sort=; DELETE FROM MyTable;
That's actually pretty easy, here's a possible approach:
<table> <tr> <th> <a href="?orderBy=type">Type:</a> </th> <th> <a href="?orderBy=description">Description:</a> </th> <th> <a href="?orderBy=recorded_date">Recorded Date:</a> </th> <th> <a href="?orderBy=added_date">Added Date:</a> </th> </tr></table><?php$orderBy = array('type', 'description', 'recorded_date', 'added_date');$order = 'type';if (isset($_GET['orderBy']) && in_array($_GET['orderBy'], $orderBy)) { $order = $_GET['orderBy'];}$query = 'SELECT * FROM aTable ORDER BY '.$order;// retrieve and show the data :)?>
That'll do the trick! :)
A SIMPLE TABLE SORT PHP CODE:
(the simple table for several values processing and sorting, using this sortable.js script )
<html><head><script src="sorttable.js"></script><style>tbody tr td {color:green;border-right:1px solid;width:200px;}</style></head><body><?php$First = array('a', 'b', 'c', 'd');$Second = array('1', '2', '3', '4');if (!empty($_POST['myFirstvalues'])) { $First = explode("\r\n",$_POST['myFirstvalues']); $Second = explode("\r\n",$_POST['mySecondvalues']);}?></br>Hi User. PUT your values</br></br><form action="" method="POST">projectX</br><textarea cols="20" rows="20" name="myFirstvalues" style="width:200px;background:url(untitled.PNG);position:relative;top:19px;Float:left;"><?php foreach($First as $vv) {echo $vv."\r\n";}?></textarea>The due amount</br><textarea cols="20" rows="20" name="mySecondvalues" style="width:200px;background:url(untitled.PNG);Float:left;"><?php foreach($Second as $vv) {echo $vv."\r\n";}?></textarea><input type="submit"></form><table class="sortable" style="padding:100px 0 0 300px;"><thead style="background-color:#999999; color:red; font-weight: bold; cursor: default; position:relative;"> <tr><th>ProjectX</th><th>Due amount</th></tr></thead><tbody><?phpforeach($First as $indx => $value) { echo '<tr><td>'.$First[$indx].'</td><td>'.$Second[$indx].'</td></tr>';}?></tbody><tfoot><tr><td>TOTAL = <b>111111111</b></td><td>Still to spend = <b>5555555</b></td></tr></tfoot></br></br></table></body></html>
source: php sortable table