Multiple record listing

4 posts by 3 authors in: Forums > CMS Builder
Last Post: May 18, 2010   (RSS)

Re: [Oakweb] Multiple record listing

By gkornbluth - May 16, 2010

Hi Oakweb,

Based on one of the recipes in my CMSB Cookbook http://www.thecmsbcookbook.com, one possible approach is that you can have separate list records calls in the body of your page that set different parameters for the separate lists of articles.

So the code for the headlines could be something like:

<?php

list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'limit' => '5',
));
?>
<table width="100" Align="left" border="0" cellpadding="5">
<?PHP foreach ($newsRecords as $record): ?>
<tr>
<td><?PHP echo $record['_link'] ?>"><?PHP echo $record['headline'] ?>
</td>
</tr>
<?PHP endforeach; ?>
</table>


And the code for the item listing could be something like

<?php

list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'limit' => '10',
));
?>
<table width="100" Align="left" border="0" cellpadding="5">
<?PHP foreach ($newsRecords as $record): ?>
<tr>
<td><?PHP echo $record['_link'] ?>"><?PHP echo $record['content'] ?>
</td>
</tr>
<?PHP endforeach; ?>
</table>


If you want to limit the number of words in the” item” listings, here’s a recipe from the Cookbook which addresses that concept.

LIMITING THE NUMBER OF WORDS SHOWN IN A PAGE
If you use the character limiting function that’s exactly what you’ll get, and words can be cut off. But if you want to limit the word count, Dave Edis from Interactive Tools shares his usual uncanny wisdom:

Put this code at the top of your page, or before you want to invoke the word limiting function:

<?PHP
function maxWords($textOrHtml, $maxWords) {
$text = strip_tags($textOrHtml);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);

return $output;
}
?>


Then put this code inside the foreach loop where you want the words limited (to 5 words):

<?PHP echo maxWords($record['content'], 5);
?>

If you want to add ...more and a link, use something like this:

<?PHP echo maxWords($record['content'], 5);
?>...<a href="<?php echo $record['_link']; ?>”>Read More</a>


If you’re using a WYSIWYG Editor and are losing some formatting when using the MaxWord function, Jason Sauchuk of Interactive Tools suggests using this version of the function instead:

<?PHP
function maxWords($textOrHtml, $maxWords) {
$text=str_replace("<p>","*P*",$textOrHtml);
$text= str_replace("</p>","*/P*",$text);
$text = strip_tags($text);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);
$output=str_replace("*P*","<p>",$output);
$output=str_replace("*/P*","</p>",$output);
$output.="</p>";

return $output;
}
?>


Hope that helps to get you started.

Best,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [Oakweb] Multiple record listing

By ross - May 17, 2010

Hi Oakweb

Thanks for posting!

I wanted to continue on Jerry's post. I think what you'll want to do with the news articles in two different lists is use two different viewers.

One could look like this:

<?php
// load headlines for side of page
list($newsRecordSide) = getRecords(array(
'tableName' => 'news',
'limit' => '5',
));
?>


and the other one could look like this:

<?php

// load headlines for middle of page
list($newsRecordsMiddle, $newsMetaDataMiddle) = getRecords(array(
'tableName' => 'news',
'limit' => '10',
));
?>


That will give you two different lists from the same table. You'll notice that in the first block I took out the meta data variable. This is usually only used if you are going to have next and previous links. Because the first block is only for the side list of articles, you won't need next and previous links.

Also, one thing I always recommend is adding comment tags above each viewer just so you can quickly see what it's trying to do instead of reading all the code every time.

Let me know if that helps. We can also have a look at your second questions if you still need more details.

Thanks!
-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

Re: [ross] Multiple record listing

By Oakweb - May 18, 2010

Many thanks for your help - all working well