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: [chris] category listing end code help needed

By Chris - August 9, 2009

An alternate approach would first gather up all the records into arrays of categories, then use two nested foreach blocks to loop over both the categories and the records belonging to each category.

Here's an example (with different variable names):

<?php
// gather up all the records into arrays of categories
$recordsByCategory = array();
foreach ($productsRecords as $record):
isset($recordsByCategory[$record['category']]) or $recordsByCategory[$record['category']] = array();
$recordsByCategory[$record['category']][] = $record;
endforeach;
?>

<ul>
<?php foreach ($recordsByCategory as $category => $categoryRecords): ?>
<li><a href="#<?php echo $category ?>"><?php echo $category ?></a></li>
<?php endforeach; ?>
</ul>

<?php foreach ($recordsByCategory as $category => $categoryRecords): ?>
<br/>
<a name="<?php echo $category ?>"></a>
<h2><?php echo $category ?></h2>

<?php foreach ($categoryRecords as $record): ?>
Record Number: <?php echo $record['num'] ?><br/>
Title: <?php echo $record['title'] ?><br/>
Content: <?php echo $record['content'] ?><br/>
_link : <a href="<?php echo $record['_link'] ?>"><?php echo $record['_link'] ?></a><br/>
<?php endforeach; ?>

<h3>Category Footer</h3>

<?php endforeach; ?>

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

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