display one recordfield as heading, simple ??

6 posts by 3 authors in: Forums > CMS Builder
Last Post: February 10, 2011   (RSS)

By kovali - February 8, 2011 - edited: February 8, 2011

Hi,

I have one section called "Wines" with 2 fields: Name and Year. When I have some records in this section like:

winename1 2010
winename2 2010
winename3 2009
winename4 2009
winename5 2009

When searching for wines of year 2009, I would like to display the results on the list page as follows:

2009 (Heading)
--------------------
winename3
winename4
winename5

But with <?php foreach ($wineRecords as $record): ?> it will always show the year 3 times also... but I only need it once (as header) [:(]

Is there a way to solve this please?
Thx,

Koen

Re: [kovali] display one recordfield as heading, simple ??

Hi Koen,

If you nsert a <?PHP break ?> in your foreach loop just before the endforeach, and that should fix the issue for you.

The break tells the loop to only execute once and then quit

There are lots of recipes on how to make CMSB do what you want it to in my CMSB Cookbook http://www.thecmsbcookbook.com

Hope that helped.

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: [gkornbluth] display one recordfield as heading, simple ??

By kovali - February 9, 2011

Thanks Jerry, perfect!

Re: [kovali] display one recordfield as heading, simple ??

By Jason - February 9, 2011

Hi Koen,

Another way of doing this could be to first organize all your records into an array sorted by year. In this example, I'll also show how you can limit the number of records you're outputting for each header.

First we sort all our records into an array by year:


$yearToRecords = array();

foreach($wineRecords as $record){

$yearToRecords[$record['year']][] = $record;
}


Next we output our records, each under the header of their year. We'll also set a limit of 3 records for each header:

<?php $maxRecordsPerHeader = 3; ?>

<?php foreach($yearToRecords as $year => $wineRecord): ?>

<h2><?php echo $year; ?></h2>

<?php $recordCount = 0; ?>
<?php foreach($wineRecord as $record): ?>

<?php $recordCount++ ;?>

<?php echo $record['name'];?> <br />

<?php if($recordCount == $maxRecordsPerHeader){ break; } ?>

<?php endforeach ?>


<?php endforeach ?>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] display one recordfield as heading, simple ??

By kovali - February 10, 2011

Thanks Jason, works perfect for me !