Checking lest entry in a foreach
            2 posts by 2 authors in: Forums > CMS Builder
Last Post: June 1, 2009   (RSS)          
By willbegood - June 1, 2009
          Hello,
lest's say i have a news section.
i've made a viewer code wich list all the entry of "news"
What i want is to check the very last entry and ad some html code in that entry
here is my code for exemple
<?php foreach ($newsRecords as $record): ?>
Record Number: <?php echo $record['num'] ?><br/>
title: <?php echo $record['title'] ?><br/>
description: <?php echo $record['description'] ?><br/>
<hr/>
<?php endforeach; ?>
So it will print this
Record Number : 1
title: abc
description : zzzzz
____________________
Record Number : 2
title: fgfgdf
description : zdfgdfghzzzz
____________________
Record Number : 3
title: abc
description : zzzzz
Here i want to dynamicaly add some content for the latest entry displayed
____________________
Can you help?
                                        
        lest's say i have a news section.
i've made a viewer code wich list all the entry of "news"
What i want is to check the very last entry and ad some html code in that entry
here is my code for exemple
<?php foreach ($newsRecords as $record): ?>
Record Number: <?php echo $record['num'] ?><br/>
title: <?php echo $record['title'] ?><br/>
description: <?php echo $record['description'] ?><br/>
<hr/>
<?php endforeach; ?>
So it will print this
Record Number : 1
title: abc
description : zzzzz
____________________
Record Number : 2
title: fgfgdf
description : zdfgdfghzzzz
____________________
Record Number : 3
title: abc
description : zzzzz
Here i want to dynamicaly add some content for the latest entry displayed
____________________
Can you help?
Re: [willbegood] Checking lest entry in a foreach
By Dave - June 1, 2009
          Hi willbegood,
Try this:
The new code is in red. Let me know if that works for you.
                          
        Try this:
<?php $totalRecords = count($newsRecords); ?>
<?php $count = 0; ?>
<?php foreach ($newsRecords as $record): ?> 
  Record Number: <?php echo $record['num'] ?><br/> 
  title: <?php echo $record['title'] ?><br/> 
  description: <?php echo $record['description'] ?><br/> 
  <?php if (++$count == $totalRecords): ?>
  This is the last record.<br/>
  <?php endif ?>
  <hr/> 
<?php endforeach; ?>The new code is in red. Let me know if that works for you.
      Dave Edis - Senior Developer
interactivetools.com
                    interactivetools.com