Previous and next records on a Detail page?

5 posts by 2 authors in: Forums > CMS Builder
Last Post: April 8, 2013   (RSS)

By 5fish - April 5, 2013

Is there a way to include links to the previous and next records on a Detail page?

I found

http://www.interactivetools.com/forum/forum-posts.php?postNum=2202277#post2202277

But doesn't seem to work for me or I am missing something.

By gregThomas - April 8, 2013

Hi, 

Here is an example of how to use the getPrevAndNextRecords function:

  // load record from 'blog'
  list($blogRecords, $blogMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'where'       => whereRecordNumberInUrl('33'),
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));

  $blog = @$blogRecords[0]; // get first record
  if (!$blog) { dieWith404("Record not found!"); } // show error message if no record found
  
  //Get prev/next links using the blog num 
  list($prevRecord, $nextRecord, $firstRecord, $lastRecord) = getPrevAndNextRecords(array( 
    'tableName' => 'blog', 
    'recordNum' => $blog['num'], 
    'orderBy'   => 'date DESC'
  ));

?>
<!-- show title of page -->
<?php echo $blog['title']; ?><br>


<!-- show previous and next links -->
Previous Record: <a href="<?php echo (@$prevRecord['_link'])? $prevRecord['_link'] : '?' ; ?>">Previous</a><br>
Next Record: <a href="<?php echo @$nextRecord['_link']; ?>" >Next</a><br>
First Record: <a href="<?php echo (@$firstRecord['_link'])? $firstRecord['_link'] : '?' ; ?>" >First</a><br>
Last Record: <a href="<?php echo @$lastRecord['_link'] ?>">Last</a><br>

I think the example code in the post you referenced was missing the orderBy statement, and that might have been why it wasn't working. I also noticed that the previous and first links return nothing if they should display the first record, and this doesn't work on modern browsers. So I've added an if statement to the links that displays an '?' if the link is empty to force the page to refresh.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gregThomas - April 8, 2013

Hi,

I think I can see the problem.

You're calling the variable $gallerytest['num'], but I can't see where that variable is being created, I think you might need to change your code to this:

<?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('/home/petersmi/public_html/','','../','../../','../../../');
  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 records
  list($gallerytestRecords, $gallerytestMetaData) = getRecords(array(
    'tableName'   => 'gallerytest',
    'where'       => whereRecordNumberInUrl(33),
    'limit'       => '1',
  ));
  $gallerytestRecord = @$gallerytestRecords[0]; // get first record

  // show error message if no matching record is found
  if (!$gallerytestRecord) {
    header("HTTP/1.0 404 Not Found");
    print "Record not found!";
    exit;
  }
 //Get prev/next links using the blog num 
  list($prevRecord, $nextRecord, $firstRecord, $lastRecord) = getPrevAndNextRecords(array( 
    'tableName' => 'gallerytest', 
    'recordNum' => $gallerytestRecord['num'], 
    'orderBy'   => 'createdDate'
  ));
?>

So I've changed the code so that it uses the $gallerytestRecord variable. Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By 5fish - April 8, 2013

Thank you!  Works great!