If I have a page of php/mysql data and it is displayed, how would I allow the user to resort this data by clicking on the header. For example, sort by price, ascending/descending by clicking on the header column that is labeled ‘price’. Or alphabetically sort by name by clicking on the header ‘name’.
I don’t want to have to resend the request to the server every time someone re-orders it. I understand that it can be done with javascript, but can’t get that to work. Any help is appreciated. I had tried using ‘orderby’ but since I’m already calling cookies that are set I wouldn’t work. Thanks.
Here is the code.
This page displays the data:
[php]<?php
function showproducts($catid, $page, $currentpage, $newpage)
{
$query = “Select count(prodid) from products where catid = $catid”;
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if ($row[0] == 0)
{
echo “
Sorry, there are no entrees in this category
\n”;}
else
{
$thispage = $page;
$totrecords = $row[0];
$recordsperpage = 5;
$offset = ($thispage - 1) * $recordsperpage;
$totpages = ceil($totrecords / $recordsperpage);
echo "<table width=\"100%\" cellpadding=\"1\" border=\"1\">\n";
echo "<tr><td><h2>Image</h2></td>\n";
echo "<td><h2>Name</h2></td>\n";
echo "<td><h2>Price</h2></td>\n";
echo "<td><h2>Restaurant </h2></td>\n";
echo "<td><h2>Phone</h2></td>\n";
echo "<td><h2>Description</h2></td>\n";
echo "<td><h2>Special</h2></td></tr>\n";
$query = "SELECT * from products WHERE catid=$catid LIMIT ORDER BY $offset,$recordsperpage";
$result = mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC))
{
$prodid = $row['prodid'];
$description = $row['description'];
$price = $row['price'];
$name = $row['entreename'];
$restname = $row['restname'];
$restphone = $row['restphone'];
$onsale = $row['onsale'];
echo "<tr><td>\n";
echo "<img src=\"showimage.php?id=$prodid\" width=\"80\" height=\"60\">";
echo "</td><td>\n";
echo "<a href=\"$newpage&id=$prodid\">$name\n";
echo "</td><td>\n";
echo "$" . $price . "\n";
echo "</td><td>\n";
echo $restname . "\n";
echo "</td><td>\n";
echo $restphone . "\n";
echo "</td><td>\n";
echo $description . "\n";
echo "</td><td>\n";
if ($onsale)
echo "On sale!\n";
else
echo " \n";
}
echo "</table>\n";
// Code to implement paging
if ($thispage > 1)
{
$page = $thispage - 1;
$prevpage = “<a href=”$currentpage&cat=$catid&page=$page">Previous page";
} else
{
$prevpage = " ";
}
if ($thispage < $totpages)
{
$page = $thispage + 1;
$nextpage = " <a href=\"$currentpage&cat=$catid&page=$page\">Next page</a>";
} else
{
$nextpage = " ";
}
if ($totpages > 1)
echo $prevpage . " " . $nextpage;
}
}
?>[/php]
Here is the page that requests the data that the page above uses:
[php]<?php
$catid = $_GET[‘cat’];
$query=“SELECT name from categories WHERE catid = $catid”;
$result = mysql_query($query);
$row=mysql_fetch_array($result, MYSQL_ASSOC);
echo “
{$row[‘name’]} - Click on an entree to learn more
\n”;
if (!isset($_GET[‘page’]))
$page = 1;
else
$page = $_GET[‘page’];
showproducts($catid, $page, “index.php?content=buyproducts”, “index.php?content=updatecart”);
?>
[/php]