I have a html table that contains product data from an sql table that is spit out with php. I'd like to sort the data by clicking the heading of the table column.
I'm outputting my table like so (php)
$product_list = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC");
$product_list .= "<tr>
<td>$id</td>
<td><strong>$product_name</strong></td>
<td>$$price</td>
<td>$category</td>
<td>$subcategory</td>
<td>$date_added</td>
</tr>";
Html
<table class="product-list">
<tr>
<th><a href='?sort=id'>ID</a></th>
<th><a href='?sort=product_name'>Name</a></th>
<th><a href='?sort=price'>Price</a></th>
<th><a href='?sort=category'>Category</a></th>
<th><a href='?sort=subcategory'>Subcategory</a></th>
<th><a href='?sort=date_added'>Added</a></th>
</tr>
<?php echo stripslashes($product_list); ?>
</table>
I've tried a few ways to do this from examples online but when I click the header nothing happens.
This is something I've tested
$sort = array('id', 'product_name', 'price', 'category', 'subcategory', 'date_added');
$order = '';
if (isset($_GET['sort']) && in_array($_GET['sort'], $sort)) {
$order .= $_GET['sort'];
}
$query = 'SELECT * FROM products ORDER BY '.$order;
// Then Output the table as above
There is a lot happening on the page, I've only included the code that generates and displays the table so it could be something else stopping this but I'd like to make sure I'm going about things the right way before delving into the rest of the code.
Any suggestions how you would go about this would be greatly appreciated.