Circular Pagination

2 posts by 2 authors in: Forums > CMS Builder
Last Post: July 3, 2013   (RSS)

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

Greg Thomas







PHP Programmer - interactivetools.com