CHOOSING WHICH RECORDS TO DISPLAY USING OFFSET AND LIMIT COMMANDS - Sep 26th, 2020
|
Nigel Gordijk from Common Sense Design needed to be able to differentiate between groups of records so that some of their associated images could be used in an image carousel and others could be presented in a list format.
It seemed complicated until CMSB user KennyH offered this simple suggestion using a combination of the limit and offset command in his list records calls.
Here’ his elegant solution:
Here we load the first 5 articles
list($article_postsRecords, $article_postsMetaData) = getRecords(array( 'tableName' => 'article_posts', 'limit' => '5', 'loadUploads' => true, 'allowSearch' => false, ));
Now, we can create add this again for the next five records, but with a few modifications:
list($article_postsSideListRecords, $article_postsSideListMetaData) = getRecords(array( 'tableName' => 'article_posts', 'limit' => '5', 'offset' => '4', 'loadUploads' => true, 'allowSearch' => false, ));
$article_postsRecords, $article_postsMetaData becomes $article_postsSideListRecords, $article_postsSideListMetaData
Add 'offset' => '4', to skip the first 5 records (numbering starts at zero)
Then you would display the first 5 images on the page like this
<?php foreach ($article_postsRecords as $record): ?>
<?php foreach ($record['images'] as $index => $upload): ?> <img src="<?php echo htmlencode($upload['urlPath']) ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt=""> <?php endforeach ?>
<?php endforeach ?>
The next 5 images pull from $article_postsSideListRecords
<?php foreach ($article_postsSideListRecords as $record): ?>
<?php foreach ($record['images'] as $index => $upload): ?> <img src="<?php echo htmlencode($upload['urlPath']) ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt=""> <?php endforeach ?>
<?php endforeach ?>
|
The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact
Interactive Tools for information on the downloading of the software or the purchasing of licenses.