HuggieBear Posted September 14, 2006 Share Posted September 14, 2006 Does anyone know if there's a tutorial which shows you how to 'order by' a particular column?I'm going to have my data displayed in a table like this:[table][tr][td]ID[/td][td]Country[/td][/tr][tr][td]1[/td][td]England[/td][/tr][tr][td]2[/td][td]Ireland[/td][/tr][tr][td]3[/td][td]Scotland[/td][/tr][tr][td]4[/td][td]Wales[/td][/tr][/table]I want the user to be able to click on the column heading and it will sort it oposite to how it's currently sorting it, similar to [url=http://www.ebuyer.com/customer/search/?strSearch=&intSubcatUID=849&bolShowAll=true&intMfrID=411]this page[/url]. If you click on the price heading it sorts it either ascending or descending, based on how it's currently displayed.I've been messing about and haven't really come close. I'll post some code examples shortly.RegardsHuggie Quote Link to comment Share on other sites More sharing options...
Orio Posted September 14, 2006 Share Posted September 14, 2006 I am not sure if I get your point, but I think what you are looking for is:[b]ORDER BY column ASC[/b] and [b]ORDER BY column DESC[/b]Orio. Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted September 14, 2006 Author Share Posted September 14, 2006 Here's what I have that works, but I wondered if there's an easier way?[code]<?php // Connect to my db include('connect.php'); // Get url params for the 'order by' if they're set if (isset($_GET['orderby']) && isset($_GET['orderdir'])){ $orderby = $_GET['orderby']; $orderdir = $_GET['orderdir']; } // Select all my records $sql = "SELECT * FROM tblCountry"; // If 'order by' was set then concatenate the string if ($orderby){ $sql .= " ORDER BY $orderby $orderdir"; } // Execute the SQL result $result = mysql_query($sql); if (!result){ echo "There was a problem selecting the countries" . mysql_error(); } // Decide what the last link pressed was and then change the next link accordingly $orderdir = ($orderdir == "desc") ? "asc" : "desc"; // Here's where I output my column headings with the links echo <<<HTML<table width="300"> <tr> <td bgcolor="#000000"> <table width="300" cellpadding="2" cellspacing="1" border="0"> <tr bgcolor="#FFFFFF"> <td width="75" align="center"><a href="datatab.php?orderby=countryID&orderdir=$orderdir">ID</a></td> <td width="225" align="center"><a href="datatab.php?orderby=countryName&orderdir=$orderdir">Country</td> </tr>HTML; while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $id = $row['countryID']; $country = $row['countryName']; echo <<<HTML <tr bgcolor="#FFFFFF"> <td width="75" align="center">$id</td> <td width="225" align="left">$country</td> </tr>HTML;} echo <<<HTML </table> </td> </tr></table>HTML;?>[/code]You can see what it looks like here: [url=http://dizzie.co.uk/php/datatab.php]Dizzie.Co.Uk[/url]RegardsHuggie Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.