displaying alternate content if no record in category

5 posts by 3 authors in: Forums > CMS Builder
Last Post: July 9, 2009   (RSS)

By Deborah - July 9, 2009 - edited: July 9, 2009

I have an editor that contains a category field. I'm listing the records for each category under a category heading on the web page. I'd like to display alternate content beneath the category heading in the event there is not at least one listing entered. I know how to show alternate content if there are no records for the entire table, but I can't figure out how to do so based on category.

Category: Electrical
<?php foreach ($links_resourcesRecords as $record): ?>
<?php if ($record['category'] != "Electrical") { continue; } ?>
<p><?php echo $record['name'] ?></p>
<?php endif ?>

<?php endforeach; ?>
<?php if (!$record['category'] != "Electrical"): ?>
<p>No listings at this time.</p>
<?php endif ?>

Using the above code, for each category on the page the listings AND the alternate content are displayed.. not one or the other. This is probably something very basic, and I'm hoping someone has a solution.

Thanks.
Deborah

Re: [Deborah] displaying alternate content if no record in category

By ross - July 9, 2009

Hi Deborah

Thanks for posting!

Could you try this if block instead of the one you are using:

<?php

if (!count($record)) {
echo "there were no categories";
}

?>


Leave the foreach loop as is. Just replace your if block with this one and let me know how you make out.

Thanks!
-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

Re: [Deborah] displaying alternate content if no record in category

By Damon - July 9, 2009

Hi Deborah,

Here is another option for you using Tech, Sports and Lifestyle as categories. With each category, you can have a custom message if there is no content for that category.

<!-- Tech starts -->
<h2>Category: Tech</h2>
<?php
$count = 0;
foreach ($newsRecords as $record):
if ($record['category'] != "tech") { continue; }
$count++;
?>
Title: <?php echo $record['title'] ?><br/>
<?php endforeach; ?>

<?php if (!$count): ?>
No tech content
<?php endif; ?>


<!-- Sports starts -->

<h2>Category: Sports</h2>
<?php
$count = 0;
foreach ($newsRecords as $record):
if ($record['category'] != "sports") { continue; }
$count++;
?>
Title: <?php echo $record['title'] ?><br/>
<?php endforeach; ?>

<?php if (!$count): ?>
No sports content
<?php endif; ?>


<!-- LifeStyle starts -->

<h2>Category: Lifestyle</h2>
<?php
$count = 0;
foreach ($newsRecords as $record):
if ($record['category'] != "lifestyle") { continue; }
$count++;
?>
Title: <?php echo $record['title'] ?><br/>
<?php endforeach; ?>

<?php if (!$count): ?>
No lifestyle content
<?php endif; ?>


Let me know how this works for you.
Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [ross] displaying alternate content if no record in category

By Deborah - July 9, 2009 - edited: July 9, 2009

Hi, Ross. Thank you for the post! I wasn't successful with editing the code. I probably did not understand fully... my error, no doubt. Here's the code I used:

[font "Verdana"]<?php foreach ($links_resourcesRecords as $record): ?>
<?php if ($record['category'] != "Electrical") { continue; } ?>
<p><?php echo $record['name'] ?></p>
<?php endif ?>
[font "Verdana"]<?php endforeach; ?>
<?php if (!count($record)) { echo "there were no categories"; } ?>
<p>No listings at this time.</p>
<?php endif ?>


Error message: Parse error: syntax error, unexpected T_ENDIF

[font "Verdana"]I solved what I needed to do by utilizing Damon's suggestion that followed yours. Sorry I couldn't make this work, but appreciate the help very much.

[font "Verdana"]Deborah