category listing end code help needed

4 posts by 2 authors in: Forums > CMS Builder
Last Post: August 9, 2009   (RSS)

By craig_bcd - August 9, 2009

Hey everyone -

I am using categories on a page that lists links. I have figured out how to list the categories and the items under the categories but I need an if statement that says, if this is the last item in the category, put in the "back to top" statement below.

I must be having a mental block because I cannot see how to determine it is the last record in the category. I think it is an "if" statement I need but i just can't get it. Any help would be appreciated.

The page is here: http://www.marcushoffman.com/links.php

Thanks!

Here is the back to top code:

<p class="back_top"><a href="#top">back to top ^</a></p>


Here is the code on the page I have so far:

<?php echo $links_introRecord['content'] ?>
<?php $lastCategory = ""; ?>
<?php foreach ($linksRecords as $record): ?>
<ul>
<li><a href="#<?php echo $record['category'] ?>"><?php echo $record['category'] ?></a></li>
</ul>
<?php endforeach; ?>

<?php if (!$linksRecords): ?>
No records were found!<br/><br/>
<?php endif ?>

<?php $lastCategory = ""; ?>
<?php foreach ($linksRecords as $record): ?>
<?php if ($record['category'] != $lastCategory): ?>
<?php $lastCategory = $record['category']?>
<br/>
<a name="<?php echo $record['category'] ?>"></a><h2><?php echo $record['category'] ?></h2>
<?php endif ?>

<p><a target="_blank" href="<?php echo $record['link'] ?>"><?php echo $record['title'] ?></a></p>

<?php endforeach; ?>

<?php if (!$linksRecords): ?>
No records were found!<br/><br/>
<?php endif ?>

Re: [craighodges] category listing end code help needed

By Chris - August 9, 2009

Hi craighodges,

With that approach, I think the easiest solution involves two if statements:

(New code shown indented)

<?php echo $links_introRecord['content'] ?>
<?php $lastCategory = ""; ?>
<ul>
<?php foreach ($linksRecords as $record): ?>
<?php if ($record['category'] != $lastCategory): ?>
<?php $lastCategory = $record['category']?>
<li><a href="#<?php echo $record['category'] ?>"><?php echo $record['category'] ?></a></li>
<?php endif ?>
<?php endforeach; ?>
</ul>

<?php if (!$linksRecords): ?>
No records were found!<br/><br/>
<?php endif ?>

<?php $lastCategory = ""; ?>
<?php foreach ($linksRecords as $record): ?>
<?php if ($record['category'] != $lastCategory): ?>
<?php if ($lastCategory !== ""): ?>
<p class="back_top"><a href="#top">back to top ^</a></p>
<?php endif; ?>
<?php $lastCategory = $record['category']?>
<br/>
<a name="<?php echo $record['category'] ?>"></a><h2><?php echo $record['category'] ?></h2>
<?php endif ?>

<p><a target="_blank" href="<?php echo $record['link'] ?>"><?php echo $record['title'] ?></a></p>

<?php endforeach; ?>

<?php if (!$linksRecords): ?>
No records were found!<br/><br/>
<?php else: ?>
<p class="back_top"><a href="#top">back to top ^</a></p>
<?php endif ?>


Hope this helps!
All the best,
Chris

Re: [chris] category listing end code help needed

By craig_bcd - August 9, 2009

Chris - that is terrific - exactly what I need! I chose the first method but I like the second one better from a programming status.

I love cmsb! It has made my life (and my customers lives) so much better.

Thanks again.

Craig