Jump to content

Order by column header


HuggieBear

Recommended Posts

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.

Regards
Huggie
Link to comment
Share on other sites

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]

Regards
Huggie
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.