Sorting Rows of an HTML Table Populated from MySQL
To sort the rows of an HTML table retrieved from a MySQL database onClick by its headers, follow these steps:
HTML Markup:
<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>
PHP Code:
In the PHP code that generates the table rows:
$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"; }
Caution:
The above is the detailed content of How to Sort HTML Table Rows Retrieved from MySQL Database on Click?. For more information, please follow other related articles on the PHP Chinese website!