Page 1 of 1

pagination with a sorted array

Posted: Fri Nov 09, 2007 10:35 am
by samusk
I am having a small problem with my pagination.

I create a sorted array with about 5 elements and they are sorted by closest distance. It works fine until I add pagination. For some reason the first page works great until I go to the next one. The algorithm to find the distance is messed up meaning the last element of distance on the previous page is not even related to the first element of distance on the next page.

I have tried a number of pagination including looking at ajax a few times.

My question is this. Instead of querying the database each time for each page, I want to create the array and sort it and use it instead of the mysql query. The problem then arises on how to code it to work with the array rather than the query.

The array is a 2d array by the way.

Any help would be greatly appreciated.

Posted: Fri Nov 09, 2007 2:40 pm
by Kieran Huggins
If you can, do all sorting inside MySQL and use LIMITS for pagination - MySQL is WAAYYYYY faster at this sort of thing.

My instinct tells me that using the Google Maps API and generating lon/lat pairs is the probably the best solution - you can then order by a function of the change in lon/lat. Hint: Pythagoras was definitely on to something.

Also, some DB engines have geographic functions that might be a tad more DRY.

Failing that, there's always the $_SESSION array to cache pre-sorted results, but that's not necessarily the *best* solution.