category listing end code help needed

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

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 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