I've got a piece of coursework to build a php site using mysql.
I need to be able to sort a table of data based on it's columns. The data is retrieved from a mysql DB using PDO. I then just echo a table while looping through each row of data
Any Ideas as to how I can sort the data by columns? Preferably I'd like to be able to sort the data (ascending) by clicking on the table headers. But i'm open to Ideas.
Code Below: PHP:
function displayBooks($data){
echo "<table id= \"allBooks\">";
echo "<th>Book ID</th><th>Title</th><th>Description</th><th>In Stock</th><th>Price</th><th>Cateogry</th>";
#foreach($data as $row)
{
echo "<tr>";
echo "<td>".$row['bookID']."</td>";
echo "<td>".$row['title']."</td>";
echo "<td>".$row['description']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>£".$row['price']."</td>";
echo "<td>".$row['name']."</td>";
echo "</tr>";
}
echo "</table>";
....
//Retrieve all books
$all_books_query = "SELECT b.bookID,b.title,b.description,b.quantity,b.price,c.name FROM Books b
INNER JOIN Book_Category bc ON bc.bookID = b.bookID
INNER JOIN Category c ON c.catID = bc.catID
GROUP BY b.bookID;";
$rows = $db->query($all_books_query)->fetchAll();
I've got a whole lot more PHP in the rest of the page. Not sure what else I need to include in this to make more sense of the problem, but any ideas will help.