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

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

By Deborah - July 9, 2009

Damon,

That worked perfectly. I like the added ability to include a unique message for each of the categories! Thank you so much for your assistance.

Deborah