Re: [aev] Records from multiple sections into one viewer
Hi aev,
This is certainly possible, but requires no small amount of PHP. Here's how to display your 9 newest records, regardless of which sections each record came from:
Firstly, generate "list page" viewer code for every section and copy the getRecords() calls from each:
list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
));
To help improve efficiency, you can add a limit (and orderBy) to each request, since you won't need more than 9 records from a single section:
list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
'limit' => '9',
'orderBy' => 'createdDate DESC',
));
To get the 9 newest records from all the sections, you'd put all the records in one list, sort them by date, and discard everything after the 9th record.
<?php
require_once "C:/wamp/www/cmsbuilder_1_31/cmsAdmin/lib/viewer_functions.php";
------------------------------8<------------------------------
*** INSERT ALL YOUR getRecord() CALLS HERE, REPLACING MINE ***
list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
'limit' => '9',
'orderBy' => 'createdDate DESC',
));
list($categoryRecords, $categoryMetaData) = getRecords(array(
'tableName' => 'category',
'limit' => '9',
'orderBy' => 'createdDate DESC',
));
--------------------------------------------------------------
// put all the records in one list
$allRecords = array_merge(
----------------------------8<-----------------------------
*** LIST ALL YOUR RECORD VARIABLES HERE, REPLACING MINE ***
$productsRecords,
$categoryRecords,
-----------------------------------------------------------
);
// sort them by date
function createdDateCompareFunction($a, $b) { return -strcmp($a['createdDate'], $b['createdDate']); }
usort($allRecords, 'createdDateCompareFunction');
// discard everything after the 9th record
$newestNineRecords = array_slice($allRecords, 0, 9);
?>
Now you can loop over them with a foreach, but beware: since your records can come from any section, they don't necessarily have the same fields!
<?php foreach ($newestNineRecords as $record): ?>
Created: <?php echo $record['createdDate'] ?><br/>
_link : <a href="<?php echo $record['_link'] ?>"><?php echo $record['_link'] ?></a><br/>
<?php endforeach; ?>
Let us know when you've got that working and we can show you how to get the 3 newest records from 3 sections.
If you have any trouble, please post your Viewer code and any error messages you're getting.
EDIT: updated usort code to work with older versions of PHP
All the best,
Chris