If a record is set to Featured, then display Featured record followed by non-featured records with limits

3 posts by 2 authors in: Forums > CMS Builder
Last Post: March 19, 2015   (RSS)

By Mikey - March 18, 2015

I'm trying to figure out a way to display a NEWS record that is set to Featured before all other NEWS records that exist as non-featured records.

In the code below I have the total records allowed to be shown set to a limit of 2.

If a News record is set to featured, then the featured NEWS record is displayed first (regardless of 'orderBy' => 'date DESC',) - followed by a single instance of a non-featured NEWS record for a total of two NEWS articles - one featured and the other not featured. If no NEWS records are set to featured - then 2 NEWS records are displayed as normal.

OR - if two NEWS records are both set as featured - then both the featured NEWS records are displayed and no other records are shown because the 'limit => '2', and 'orderBy' => 'date DESC', functions as normal.

However if the 'limit => '5', was changed later to 5... then the records shown would be the 2 featured News records displayed first (regardless of 'orderBy' => 'date DESC',) , followed by 3 additional non-featured NEWS records followed by NEWS set to 'orderBy' => 'date DESC',.

OR if no NEWS records are set to featured - then 5 NEWS records are displayed as normal and 'orderBy' => 'date DESC', functions as normal.

Anyone have any suggestions on how to get this working?

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  
  // load viewer library
  $libraryPath = 'cmsBuilder/lib/viewer_functions.php';
  $dirsToCheck = array('/home/1234/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 from 'news'
  list($newsRecords, $newsMetaData) = getRecords(array(
    'tableName'   => 'news',
    'limit'       => '2',
    //'limit'       => '5',
    //'where' => 'featured = 1',
    'where' => " featured = '1' OR featured = '0' ",
    'loadUploads' => true,
    'allowSearch' => false,
    'orderBy'     => 'date DESC', 
  ));

?>
<?php require_once("maxWords.php"); ?>

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>News</title>
</head>

<body>

<?php foreach ($newsRecords as $record): ?>
<li class="thenews">
<h4><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></h4>
      <?php if ($record['summary']): ?>
       <p><?php echo htmlencode($record['summary']) ?></p>
    <p><a href="<?php echo $record['_link'] ?>">Continue...</a></p>
      <?php else: ?>
<p><?php echo maxWords($record['content'], 36); ?></p>
<p><a href="<?php echo $record['_link'] ?>">Continue...</a></p>
      <?php endif ?>
<!-- /thenews --></li>
<?php endforeach ?>

</body>
</html>

Thanks for any help you can offer up - Zicky

By Damon - March 19, 2015

Hi Zicky,

Would using this create the results you are after?

'limit'       => '5',
'orderBy'     => 'featured DESC, date DESC', 

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/