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 gkornbluth - May 17, 2013
Hi mizrahi,
Here's a recipe from my CMSB Cookbook ( http:www.thecmsbcookbook.com ) that might help:
SORTING BY GROUP AND INSERTING GROUP HEADINGS IN A LIST VIEWER
If you’d like to display group headings on your list page here’s one approach:
For this example, a multi-record editor, called “events” has the following fields:
Title (a text field)
Type (a pull down list of “Groups” to minimize entry errors)
Starting Date (a date field) * in the viewer, only the Month, Day and Year are visible
Preview (a short description of the event for the listing page)
In your application, you could add a full description of the event for the details page, images, etc.
The sort is by Type (ASC)ending and then by Start Date (ASC)ending
On the list viewer page the following code appears where you want your list to appear .
<?php
$old_group = ''; // init blank var.
foreach ($eventsRecords as $record):
$group = $record['type']; // load sub-group value from record.
if ($group != $old_group) { // If different from the last sub-group value, print the sub-group name.
echo "<h2>$group</h2>";
}?>
<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a>
<?php echo $record['preview'] ?>
<br /><br />
<?php $old_group = $group; // retain sub-group name before moving to new record. ?>
<?php endforeach ?>
To display images instead of text for the headings, one approach would be to create a separate single record editor called “Graphics” with each image as a separate upload field named Group 1 Graphic, Group 2 Graphic, etc., and use something like this on the viewer page (don’t forget to add the getrecord call at the top of your page).
<?php $old_group = ''; ?>
<?php foreach ($eventsRecords as $record): ?>
<?php $group = $record['type']; ?>
<?php if ($group != $old_group && $record['type'] == "Group1"): ?>
<div align="center"><?php foreach ($graphicsRecord['group_1_graphic'] as $upload): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt='' /><br /><br /></div>
<?php endforeach ?> <?php endif ?>
<?php if ($group != $old_group && $record['type'] == "Group 2"): ?>
<div align="center"><?php foreach ($graphicsRecord['group_2_graphic'] as $upload): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt='' /><br /><br /></div>
<?php endforeach ?> <?php endif ?>
<?php if ($group != $old_group && $record['type'] == "Group 3"): ?>
<div align="center"><?php foreach ($graphicsRecord['group_3_graphic'] as $upload): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt='' /><br /><br /></div>
<?php endforeach ?> <?php endif ?>
Hope that gives you a head start,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
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
PHP Programmer - interactivetools.com