Forum Moderators: coopster

Message Too Old, No Replies

Pagination issue

Any Ideas on how I might handle this small problem?

         

Bluesprocket

3:37 pm on Nov 19, 2008 (gmt 0)

10+ Year Member



Hi all! I have an issue that Im sure is a simple fix but it is escaping me for some reason...

I am building a site that displays properties for rent. I have a simple display.php page that pulls all the property data from MySQL and displays it. Here is my issue..

I need a prev / next function on the page so we can scroll through the listings. The record ID's are not 1,2,3 for example...they are more like 33, 46, 95, 105.

Most pagination script assume you are starting at the begining of the list of entries and do

$currid = $id;
$previd = $id -1;
$nextid = $id +1;

etc, etc,

As the ID's are not in order like that, I am not sure how to go about this. What I need is to do this (going by my previous example).....

$currid = 46; // for example
$previd =33;
$nextid =95;

As the records are added and deleted all the time, I need this to find it dynamically on the page as we go. Any suggestions? I would really appreciate it. Thanks

Scott

enigma1

3:44 pm on Nov 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Pagination scripts rely on the sql query to do the splitting. See how phpmyadmin for instance does it when you browse a table with many rows.

Watch the "limit 0, 30" "limit 30, 30", "limit 60, 30" and so forth as you go page-1 to page-2 to page-3 respectively having the defaults.

This is basically what you need to pass to your sql query along with everything else, to achieve splitting regardless of custom ids.

cameraman

4:02 pm on Nov 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can either run two queries to find the next number and previous number when the page loads, or, what I'd do, set the 'next' link to be '46n' and the 'previous' link to be '46p'. Then you dissect it when the page loads so you only have to do one query: id > 46 if the last character is 'n' and id < 46 if the last character is 'p'. You can use the substr function to strip the last character or a regular expression like
preg_match('#(\d+)(.)#',$_GET('page'),$page);

$page[1] will contain the id you're coming from, and $page[2] will contain the 'n' or 'p'. I'd use the regular expression once instead of substr() twice (once to get the last character and once to get everything before the last character).

Bluesprocket

4:16 pm on Nov 19, 2008 (gmt 0)

10+ Year Member



Thanks guys I got it. I appreciate the input.