Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Sorting & Pagination

    I'm planning to query a table, and to create a table/grid of approximately 12 or 14 columns. Creating the initial query and the initial order by shouldn't be to difficult to figure out.

    But than I would like to be able to re-sort the data based on the various columns asc/desc which clicking each column header. Preferably I would also like to have the data paginated into multiple 'pages'.. ie, a query of 100 rows of data would be 4 pages of 25, etc.

    Can this be done running the query once, building the array, and than re-sorting the array based on the columns sorts? Or does anyone have any basic samples on beginning to set it up?

    Thanks
    PT

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This can be done using an array; however, the only way to do it would be to fetch every record into this array to start with. This may be a huge burden in the amount of processing / memory used for this simple task.
    Pagination works by (preferably) reducing the size of the result set to a limited value of 25 records for example. You would need to re-query the data with you're new order by if you're using the LIMIT (or TOP, or whatever controls depending on you're engine in use).
    From a pure PHP perspective, this is impossible. It will limit you to 25 records and attempting to sort it would result in a requery anyway. Because of this, its best to just make use of LIMIT calls.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Jay has a pagination class on his site:

    http://www.jaygilford.com/php/comple...ination-class/

    Even if that doesn't exactly suit your needs, it'll give you an idea of how to go about things.

  • #4
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    If you don't want to have to re-query your database, you'd have to use javascript to store all of your results and do all of the sorting. This would be a nightmare, especially if you have any significant number of results.

    The best way to do this is to do a new query every time you sort or go to a new page. You don't have to mess with javascript, which is slow and not well-suited for this task. Use Javascript, instead, to do asynchronous posts to a PHP script to prevent full page loads. It should still be very quick.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •