Dynamic Table Sorting - Help!!!

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
tomex1
Forum Commoner
Posts: 26
Joined: Sun Aug 16, 2009 11:34 am

Dynamic Table Sorting - Help!!!

Post by tomex1 »

Hi guys,
I have been learning php for some few weeks now and I must say I am getting better. At the moment, I am building an application which queries a database based on the values filled on a form and displays the result on another page when the user clicks the submit button.
The results are displayed in a tabular form with Column headings which works fine. Here is my problem - I want to give users the ability to sort the results based on the column headings. For example, when he clicks on the 'Name' column, the results should be sorted alphabetically.
I came up with the idea of redirecting the page to itself (i.e. the results page) and running an sql statement basing the ORDER BY on the column selected by the user. Obviously, what I didn't realise was that the values passed to the results page from the form using POST would have been lost, so I keep getting errors complaining about those values. I tried to create hidden fields and hide these values into them but it doesn't seem to work. I did a bit of research and I discovered php Sessions.
My question is - do you think I should use sessions to achieve this or is there a better way? Any information would be greatly appreciated.

Thanks.
SpankMarvin
Forum Newbie
Posts: 17
Joined: Mon Jul 27, 2009 1:08 am

Re: Dynamic Table Sorting - Help!!!

Post by SpankMarvin »

I'm sure there is a better way of doing this than my suggestions, but there are a number of ways to bring over the values from the $_POST initial submittions.

The first is to use sessions to store the data in $_SESSION array. This has the advantage of being carriable over several pages until the session is destroyed. Alternatively, and if the info does not need to be secure, you could pass the data through to the same page using a GET method so that the values would be carried over through the URL (http://www.yoursite.com/page.php?firstv ... =something). You would need to look for these values in the page though.

There are also javascript solutions which might be a better way to go, as you'd not be querying the database multiple times.

Hope that helps

J
tomex1
Forum Commoner
Posts: 26
Joined: Sun Aug 16, 2009 11:34 am

Re: Dynamic Table Sorting - Help!!!

Post by tomex1 »

Hi J,
Many thanks. I'll look into the various options you mentioned. You talked about a javascript option - how does that work? I'd prefer to go for the best solution as it will also be a learning curve for me.

Cheers
SpankMarvin
Forum Newbie
Posts: 17
Joined: Mon Jul 27, 2009 1:08 am

Re: Dynamic Table Sorting - Help!!!

Post by SpankMarvin »

I'm not sure how hot you are on jQuery but this looks good:

http://tablesorter.com/docs/

I haven't used it but from the demo, it looks like what you're after, and no additional queries required.

Good luck

J
Post Reply