Circular Pagination
2 posts by 2 authors in: Forums > CMS Builder
Last Post: July 3, 2013 (RSS)
By Perchpole - July 2, 2013
Hi, All
I am trying to implement a sort of circular pagination system for a client using prev/next links. He wants people to skip from page to page in an endless loop.
All of the pages assigned to a category would be included in the loop (excluding those which are hidden).
In this example each Car page is assigned to the Car category:
Car Category
Car 1
Car 2 [hidden]
Car 3
Car 4
If the reader were on the "Car 1" page the prev/next links would be:
<< Prev:Car 4 & Next:Car 3 >>
Once the reader reaches "Car 4" the links would be:
<< Prev:Car 3 & Next:Car 1 >>
The reader could then keep clicking on the Next link to go from page to page to page without ever reaching the end.
:0/
Perchpole
By gregThomas - July 3, 2013
Hi Perchpole
The best way to do this is using the CMSB getPrevAndNextRecords function, and then use it's output to decide if the prev or next links should link back to the first or last record:
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
/* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('C:/wamp/www/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
// load record from 'blog'
list($blogRecords, $blogMetaData) = getRecords(array(
'tableName' => 'blog',
'where' => whereRecordNumberInUrl(0),
'loadUploads' => true,
'allowSearch' => false,
'limit' => '1',
));
$blogRecord = @$blogRecords[0]; // get first record
//Set options for getPrevAndNextRecords function
$optionsArray = array(
'tableName' => 'blog',
'where' => 'hidden = 0',
'recordNum' => $blogRecord['num']
);
list($prevRecord, $nextRecord, $firstRecord, $lastRecord) = getPrevAndNextRecords($optionsArray);
//Create previous title and link variables.
if($prevRecord){
$previousLink = $prevRecord['_link'];
$previousTitle = $prevRecord['title'];
}else{
$previousLink = $lastRecord['_link'];
$previousTitle = $lastRecord['title'];
}
//Create next title and link variables
if($nextRecord){
$nextLink = $nextRecord['_link'];
$nextTitle = $nextRecord['title'];
}else{
$nextLink = $firstRecord['_link'];
$nextTitle = $firstRecord['title'];
}
?>
<!-- display page content -->
<h1><?php echo $blogRecord['title']; ?></h1>
<?php echo $blogRecord['content']; ?>
<!-- display prev / next buttons -->
<a href="<?php echo $previousLink; ?>" >Previous <?php echo $previousTitle; ?></a>
<a href="<?php echo $nextLink; ?>" >Next<?php echo $nextTitle; ?></a>
This is just example code, so you'll have to make a few changes to get it working with your site. You might also have to add the category to the where statement for the getPrevAndNextRecords function as well.
So the contents of the detail page is retrieved using the standard getRecords function. Then the first, last, previous and next records are retrieved using the getPrevAndNextRecords function. If the record is the first or last in the list the $lastRecord and $nextRecords variables will be empty arrays. If they are then the first or last record details are used instead in the prevous and next links.
Let me know if you have any questions.
Thanks!
Greg
PHP Programmer - interactivetools.com