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

By kovali - February 9, 2011

Thanks Jerry, perfect!

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

By kovali - February 9, 2011

Hello Jerry, one more question if possible...

With the <break> function I can now show only one record, perfect. But is there a way to show only 3 records out of 20 let's say, without using the Limit => 3 function ??

I need this on a page where I want to show all records (without limit...) and once again only the 3 most recent records of the same section at the top of the page... When i use Limit => 3 it will only show 3 records in both lists ofcourse...

Thx,
Koen

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 !