Need to present News Archive by year

3 posts by 3 authors in: Forums > CMS Builder
Last Post: May 20, 2013   (RSS)

By mizrahi - May 17, 2013

I have a news archive that I need to present by year  and I am not sure how to go about doing this. Here is the basic code I am using now to display the records in a straight listing:

// archived news items
list($archived_newsRecords, $archived_newsMetaData) = getRecords(array(
'tableName'   => 'en_us_news_items',
'loadUploads' => true,
'allowSearch' => false,
'where'    => "archive=1",
));

<?php foreach ($archived_newsRecords as $record): ?>
   <h4>
      <a href="<?php echo htmlencode($record['_link']) ?>">
         <?php echo htmlencode($record['title']) ?>
      </a>
   </h4>
   <p class="date">
      <?php echo date("F j, Y", strtotime($record['publishDate'])) ?>
   </p>
<?php endforeach ?>

By gregThomas - May 20, 2013

Hi, 

To change the order a getRecords function returns its results in you can add a orderBy value to array. This should sort your results by the publishDate field:

// archived news items
list($archived_newsRecords, $archived_newsMetaData) = getRecords(array(
  'tableName'   => 'en_us_news_items',
  'loadUploads' => true,
  'allowSearch' => false,
  'where'       => "archive=1",
  'orderBy      => "publishDate DESC"
));

This adds an ORDER BY statement to the end of the MySQL statement that will order the items by the publishDate field in descending order.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com