making a "go to next article" link on detailed article page

6 posts by 2 authors in: Forums > CMS Builder
Last Post: August 27, 2013   (RSS)

By meg - August 23, 2013

I'm using a multi-listing section as a blog and I need to create a "go to next article" link, more specifically previous and next links, on a detailed blog page. I thought I resolved this problem with a project, but taking a closer look, I guess I didn't. This is what I have. Please tell me what I need to change - thank you!

I believe I have this set up as a combo page.

list($articlesRecords, $articlesMetaData) = getRecords(array(
    'tableName'   => 'articles',
    'where'       => whereRecordNumberInUrl(1), 
    'loadUploads' => true,
    'allowSearch' => true,
    'limit'       => '1',
  ));
  $detailRecord = @$articlesRecords[0];
  
  list($articlesRecords, $articlesMetaData) = getRecords(array(
    'tableName'   => 'articles',
    'loadUploads' => false,
    'allowSearch' => false,
'perPage'     => '1',
  ));

<!-- PREVIOUS -->
<div><?php if ($articlesMetaData['prevPage']): ?><a href="<?php echo $articlesMetaData['prevPageLink'] ?>">PREVIOUS FEATURE</a> <?php else: ?>PREVIOUS FEATURE<?php endif ?></div>

<!-- NEXT -->
<div><?php if ($articlesMetaData['nextPage']): ?><a href="<?php echo $articlesMetaData['nextPageLink'] ?>">NEXT FEATURE</a><?php else: ?>NEXT FEATURE<?php endif ?></div>

Hi Meg, 

This is can be done using getPrevAndNextRecords function. I wrote an example of how to use it in this post:

http://www.interactivetools.com/forum/forum-posts.php?postNum=2230133#post2230133

Let me know if you need a hand integrating it into your code.

Cheers!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Meg,

You've assigned the details from your articles section to $detailRecord. You just need to amend $articlesRecord['num'] to $detailRecord['num']:

<?php
  $libraryPath = 'cms/lib/viewer_functions.php';
  $dirsToCheck = array('/home/content/85/8004985/html/themarrowmagazine/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  list($articlesRecords, $articlesMetaData) = getRecords(array(
    'tableName'   => 'articles',
    'where'       => whereRecordNumberInUrl(1), 
    'loadUploads' => true,
    'allowSearch' => true,
    'limit'       => '1',
  ));
  $detailRecord = @$articlesRecords[0];
  
  list($prevRecord, $nextRecord, $firstRecord, $lastRecord) = getPrevAndNextRecords(array( 
    'tableName' => 'articles', 
    'recordNum' => $detailRecord['num'], 
    'orderBy'   => 'createdDate'
  ));
  
  list($articlesRecords, $articlesMetaData) = getRecords(array(
    'tableName'   => 'articles',
    'loadUploads' => false,
    'allowSearch' => false,
'perPage'     => '1',
  ));
  

?>

This will return the previous and next record details for selected article number.

Cheers!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By meg - August 26, 2013

The links work great! However, I would like to have them deactivated when there's no next or previous. Is there a way to do that? I tried this, but the previous button was never activated.

<!-- PREVIOUS -->
<div class="previous-article"><?php if ($articlesMetaData['prevPage']): ?><a href="<?php echo (@$prevRecord['_link'])? $prevRecord['_link'] : '?' ; ?>" style="background: none;"><span style="font-size: 12px"><</span> PREVIOUS FEATURE</a> <?php else: ?><span style="font-size: 12px"><</span> PREVIOUS FEATURE<?php endif ?></div>

<!-- NEXT -->
<div class="next-article"><?php if ($articlesMetaData['nextPage']): ?><a href="<?php echo (@$nextRecord['_link'])? $nextRecord['_link'] : '?' ; ?>" style="background: none; text-align: right;">NEXT FEATURE <span style="font-size: 12px">></span></a><?php else: ?>NEXT FEATURE <span style="font-size: 12px">></span><?php endif ?></div>

Hi Meg,

You just need to change the code so that it checks if data from the prevRecord and nextRecord exists. As the $articleMetaData['prevPage'] will always have a link even if it's to a blank page. Something like this should work:

<!-- PREVIOUS -->
<div class="previous-article">
  <?php if (@$prevRecord['num']): ?>
    <a href="<?php echo (@$prevRecord['_link'])? $prevRecord['_link'] : '?' ; ?>" style="background: none;"><span style="font-size: 12px"><</span> PREVIOUS FEATURE</a>
  <?php else: ?>
    <span style="font-size: 12px"><</span> PREVIOUS FEATURE
  <?php endif ?>
</div>

<!-- NEXT -->
<div class="next-article">
  <?php if (@$nextRecord['num']): ?>
    <a href="<?php echo (@$nextRecord['_link'])? $nextRecord['_link'] : '?' ; ?>" style="background: none; text-align: right;">NEXT FEATURE <span style="font-size: 12px">></span></a>
  <?php else: ?>
    NEXT FEATURE <span style="font-size: 12px">></span>
  <?php endif ?>
</div>

If there isn't a previous or next record, the system will return an empty array, so we can check for the num value in each variable to see if the array has been created.

Let me know if you have any questions.

Cheers

Greg

Greg Thomas







PHP Programmer - interactivetools.com