List page - if category not in URL, display alternate heading

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

By Deborah - April 2, 2014

I've searched the form, but am not able to find this topic. What I'm trying to achieve is:

  1. If the URL to the list page viewer contains a category num, I would like the page heading on the list viewer page to display 'CategoryName News'.
  2. If the URL does not contain a category num, I'd like the page heading to display just 'News'.

What's happening with my current code is that if there is not a category num in the URL, the page displays the last updated category title instead of nothing.

LINKS TO VIEWER PAGE:

A) Display news articles filtered by category in URL:
<a href="/news/index.php/category-<?php echo ($record['num']) ?>/">
(Shows selected page title category, as intended.)

B) Display all news articles:
<a href="/news/index.php">
(Shows a page title even if no category is in URL.)

VIEWER PAGE CODE:

---- HEAD ----
// 'news' list
list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'orderBy' => "posting_date DESC",
));
// category title for pg heading
list($newsRecordsHDG, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'limit' => '1',
'where' => whereRecordNumberInUrl(1),
'allowSearch' => true, // also tried as false
));

---- HTML ----
<?php if ($newsRecordsHDG): ?>
<?php foreach ($newsRecordsHDG as $record): ?><?php echo $record['category:label'] ?><?php endforeach ?>
<?php endif ?> News

Desired HTML output:

If category num in URL = 'Category News'

If no category num in URL = 'News'

I hope I've explained myself well enough. Thanks for looking and especially for any help!
~ Deborah

By Chris - April 3, 2014

Hi Deborah,

The argument to whereRecordNumberInUrl() is the default where clause used if there is no number in the URL. whereRecordNumberInUrl(1) is the same as whereRecordNumberInUrl("TRUE"), it defaults to WHERE TRUE, so it will find the first record.

whereRecordNumberInUrl("FALSE") or whereRecordNumberInUrl(0) will not return a record if there is no number in the URL.

This should work:

---- HEAD ----
// 'news' list 
list($newsRecords, $newsMetaData) = getRecords(array( 
  'tableName' => 'news', 
  'orderBy' => "posting_date DESC", 
)); 
// category title for pg heading 
list($newsRecordsHDG, $newsMetaData) = getRecords(array( 
  'tableName' => 'news', 
  'limit' => '1', 
  'where' => whereRecordNumberInUrl("FALSE"), // if no record number in url, where = FALSE
  'allowSearch' => false,
));

---- HTML ----
<?php if ($newsRecordsHDG): ?>
  <?php foreach ($newsRecordsHDG as $record): ?><?php echo $record['category:label'] ?><?php endforeach ?>
<?php else: ?>
  News
<?php endif ?>

Does that help?

All the best,
Chris

By Deborah - April 13, 2014

Chris, I just realized I hadn't replied. Your solution worked perfectly!  I was always a bit mystified about the differences in use of whereRecordNumberInUrl(), so your explanation was also helpful.

Thanks so much for your time and expertise!
Deborah