Skipping specific records when using [previous] & [next] code

11 posts by 2 authors in: Forums > CMS Builder
Last Post: April 30, 2014   (RSS)

By drewh01 - April 29, 2014

I have a set of records where on the detail page I would like to use [previous] & [next] code, but I have some placeholder records that I am using to label certain sections and I do not want them to appear in the [previous] & [next] line up.

How can I keep a record from appearing when using the [previous] & [next] links?


<?php if (@$prevRecord['num']): ?><a href="<?php echo (@$prevRecord['_link'])? $prevRecord['_link'] : '?' ; ?>">&lt;&lt; prev film</a><?php endif ?>


<?php if (@$nextRecord['num']): ?><a href="<?php echo @$nextRecord['_link']; ?>" >next film &gt;&gt;</a><?php endif ?>

Thanks!

d

By Chris - April 29, 2014

Hi drewh01,

I'm not sure I quite understand what you're doing. Can you post the complete PHP source code for your page? Preferably as an attachment?

All the best,
Chris

By drewh01 - April 29, 2014

I really don't have much more code to show other than what I pasted in my original message.

What I am trying to do is to use the dynamic [previous] and [next] links to navigate from one record detail page to the next and back.

I am looking for a way to set a specific record to be skipped when navigating through the previous and next links.

For instance:

[previous]

Record 1
Record 2  <---- Skip this detail page
Record 3
Record 4
Record 5

[next]

By Chris - April 29, 2014

Hi drewh01,

I'm assuming you're using getPrevAndNextRecords()? You can use a where clause to filter certain results.

// Supported options: tableName (required), recordNum (optional), and orderBy (optional, defaults to schema listPageOrder)
// list($prevRecord, $nextRecord, $firstRecord, $lastRecord) = getPrevAndNextRecords(array(
// 'tableName' => 'news',
// 'recordNum' => $record['num'],
// 'where' => '1', // optional - defaults to all records in section
// 'orderBy' => 'createdDate', // optional - defaults to schema listPageOrder
// ));

For example:

$where = "num != 2";

list($prevRecord, $nextRecord, $firstRecord, $lastRecord) = getPrevAndNextRecords(array(
  'tableName' => 'news',
  'recordNum' => $record['num'],
  'where'     => $where,
));

Does that help?

All the best,
Chris

By drewh01 - April 29, 2014

Thanks!  I'm a bit rusty but here are some of my load records:

What is the best way to add the "where" code?

// load record from 'films'
list($filmsRecords, $filmsMetaData) = getRecords(array(
'tableName' => 'films',
'where' => whereRecordNumberInUrl(0),
'loadUploads' => true,
'allowSearch' => false,
'limit' => '1',
));
$filmsRecord = @$filmsRecords[0]; // get first record
if (!$filmsRecord) { 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' => 'films',
'recordNum' => $filmsRecord['num'],
'orderBy' => 'createdDate'
));

By Chris - April 29, 2014

Here:

//Get prev/next links using the blog num 
$where = "num != 2";
list($prevRecord, $nextRecord, $firstRecord, $lastRecord) = getPrevAndNextRecords(array( 
'tableName' => 'films', 
'recordNum' => $filmsRecord['num'], 
'where'     => $where,
'orderBy'   => 'createdDate'
));

Does that help?

All the best,
Chris

By drewh01 - April 29, 2014 - edited: April 29, 2014

Thanks, that really helps.

Finally - how do I change to using one of the fields rather than "num"?

$where = "num != 2";

$where = "title != ??";

For instance, I want only the records that have a title to appear in the previous & next function.

Is this even possible, or is there a better way?

By Chris - April 30, 2014

Hi drewh01,

Try this:

$where = "title > ''";

Usually, you can get away with title != '', but that won't catch NULL fields, so the above is safer.

Does that help?

All the best,
Chris

By Chris - April 30, 2014

Yes, if you omit the 'orderBy' line, getPrevAndNextRecords() will default to the sort order defined in the Admin.

All the best,
Chris