PHP Paginate & Sort

Associate
Joined
11 Mar 2007
Posts
1,741
Evening all,

Little help if you don't mind.

I'm pulling variables from a MySql DB and displaying them in a table. Easy. I've then got 2 drop down fields for sorting, each with their own submit button going via $_GET, (doesn't need to be secure). One filters the results and the other sorts them by date.

I can filter the results then sort them by date by including a hidden field in the form that gets the location variable if it exists and passing it along again if the date submit button is pressed.

All ok.

Now I want to implement pagination. Which I managed. Fairly standard I think, counts the total rows, works out which page we should be on, adjusts the MYSql query accordingly.

But how the hell do I get the 2 to work together? I can sort & filter the first page but after that it forgets, (obviously).Thought about using sessions, but seems overly complicated, and couldn't think of a way to implement them satisfactorily.

I guess the answer is to have it so that when the filter and sort forms are submitted it grabs the variables from the $_GET and uses them to make the new pagination links, but for some reason I seem to be struggling and getting bogged down in IF statements.

If anybody could link to a good tutorial, post up some sample code or tell me where i'm going wrong I'd really appreciate it.

Thanks
 
GET /page.php?sort=x&filter=y&page=z

select count(*)...where sort = x and filter = y

Divide into pages

Then:

select...where filter = y, ORDER BY x LIMIT (page * offset), numperpage

Written badly because it's late but hopefully useful and I hope I haven't mixed up the LIMIT parameter order!

Essentially you need to keep the variables in the URL, keeping them in GET seems simplest, then run the queries each time. In terms of figuring what to view/delete/edit however, each row in the db should have a unique ID (primary key) which you can reference in the URL rather than trying to work out which on the user clicked (eg if the table changes - you'd delete the wrong row!)
 
Back
Top Bottom