Sorting a table in runtime with click on title

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Sorting a table in runtime with click on title

Post by technofreak »

I have been trying to find a solution for my problem. I have a customer database and i display the data from the database in a table. The first row of the table carries the title of respective columns.

My requirement is, when i click one of the title, the contents of the entire table ie all the rows should be sorted based upon this field. For initial consideration, say the sorting is done in ascending order based upon the field title being clicked. How can i do this ?

I know that client side actions can be done using Javascripts, but am not that much exp in java to write a script for this. This kind of interactive table sorting is present in certain mail clients like yahoo, where you can sort your mails shown in your inbox by clicking over any of the field titles.

It would be very usefull if am guided upon this, atleast with links where i can get to know how to do this ? [:)]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are you wanting a PHP or Javascript solution? Since you posted in the PHP Code board, I can only assume a PHP solution. Therefore, there is a post referenced from Useful Posts about doing exactly this.
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post by technofreak »

Feyd,

As i said that I am not very much knowledged about javascripts, am looking for a PHP solution only. I will check the Useful posts :)
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post by technofreak »

Feyd,

so the workaround is

1. When displaying the table, make the <th>field-names</th> to be links with a variable specifiying the corresponding field-name to the target page

2. In the target page, $_GET the passed variable and based upon this, change the SORT By or ORDER By value in the MySQL query so that the correspding result is fetched again, having been sorted out the way we want.

3. Display the same table with the new contents (ie sorted out contents) in the 2nd page.

I will try this out and get you back. Thanks, that useful post link was really usefull and is a simple solution to my problem :)
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post by technofreak »

this is the method i followed and it worked :) I made some silly mistakes before making this work.. :D

Code: Select all

<div><table align='center' border='1'>
<tr>
<?php while ($field = mysql_fetch_field($q)){
$key = $field->name;
echo $key;
print "  <th><a href="view_user_sort.php?key=$key">$field->name</a></th>\n";
} ?>
</tr>
i try to declare the field name in each case to $key variable and use this variable in the href as url?keys=$keys. The value of this variable will help in knowing which field-name has been selected to be sorted, so i can use a sql query with "ORDER BY $keys". Now i have to test whether the mysql queries on the second page work properly :)
Post Reply