Custom Web Publishing: Paginate Your Results

| 1 Comment

<-Previous | Next ->

When you click next or previous links on a page of search results, you're asking the page to conduct the original search all over again, but this time return a different portion of the results. The portion is determined by skip and max values. Skip tells the server how many records you don't want to see. Max is the number of records you want to see at one time. If the skip value is 15 and the max is 3, the page will display the 16th, 17th and 18th records.

Here's our search with the setRange command added. The skip value is 4 and the max is 2 so this will display the 5th and 6th records in our sorted found set.

$findCommand =& $fm->newFindCommand('web_AssetSearch');
$findCommand->addFindCriterion('rt_Type', 'Phrasebook');
$findCommand->addFindCriterion('rt_Subject', 'Travel');
$findCommand->addSortRule('rt_Format', 1, 'FormatList');
$findCommand->addSortRule('rt_TitleEnglish', 2, FILEMAKER_SORT_DESCEND);
$findCommand->setRange(4, 2);
$result = $findCommand->execute();

But you would never hard code skip and max values like the example above. Instead you create variables to calculate incremental values as the user clicks through results. Paste the following code above the search block:

$skip = $_GET['skip'];
if(!isset($skip)) { $skip = 0; }
$max = 5;

Ever seen a skip in a url? As you click through search results you might see something like this in the address bar:

...searchresult.php?skip=4&type=Phrasebook...

$skip = $_GET['skip']; plucks the skip value out of the url (in this case a 4) and stores it in a variable called $skip.

What if there's no skip in the url? Maybe you just left the search page and are seeing the first page of results. if(!isset($skip)) { $skip = 0; } checks for a skip and sets it to zero if it's not there.

$max = 5; Even though max doesn't increment from page to page, it's good practice to set it to a variable.

Now use the variables in the search block.

$findCommand->setRange($skip, $max);

You're not done yet. Now calculate skip values for next and previous links. These values will be greater or less than the current skip by a margin of max. First we need to know the total number of matching records — found count in FileMaker parlance. After the line that dumps the search results into an array, add a line that counts the results, like this:

$records = $result->getRecords();
$found = $result->getFoundSetCount();

Now calculate prev and next. We're trapping prev values that dip below zero and next values that exceed the found count.

$prev = $skip - $max;
$next = $skip + $max;
if(($skip + $max) > $found) {$next = $skip; }

Build next and previous links.

<a href="<?php echo "?skip=$prev"; ?>">Previous</a> | 
<a href="<?php echo "?skip=$next"; ?>">Next</a>

Next ->

1 Comment

Here's a little trick I use to hide the Previous and Next links if they're not needed. It also determines if the separator bar is necessary. Start by inserting this line after you calculate $prev and $next values:


if($prev >= 0 && ($skip + $max) < $found )
{ $sepbar = " | "; } else { $sepbar = ""; }


Now add similar conditionals to the next and previous links:

<?php if($prev >= 0) { ?>
<a href="<?php echo "?skip=$prev"; ?>">Previous</a>
<?php } ?><?php echo $sepbar; ?><?php if(($skip + $max) < $found) { ?>
<a href="<?php echo "?skip=$next"; ?>">Next</a>
<?php } ?>

Leave a comment