display '0' if field exists, but not approved

4 posts by 2 authors in: Forums > CMS Builder
Last Post: February 28, 2011   (RSS)

By Deborah - February 27, 2011

A client's blog page contains a comments form with posts that are moderated by the admin. For posts that are approved, we are displaying the total number of approved comments, such as " Comments (5) ".

While I can display the total number of approved comments, this total does not reflect '0', because (as I understand it) PHP is is counting only the comments that are approved.

This code successfully displays approved comments:

<?php foreach ($commentsRecords3 as $record): // total number of approved comments ?>
<?php if ($record['status'] == "1"): ?>
<?php $recordsReturned = count ($commentsRecords3); ?>
<p class="subhd">comments (<?php echo ($commentsMetaData['totalRecords']); ?>)</p>
<?php endif ?>
<?php endforeach; ?>

If there are (either comments entered or not entered) no comments approved, I would like to display a default message, such as " Comments (0) ".

My 'elseif' attempts resulted in no text display. Any tips? I feel it is so simple, but the solution eludes me... still learning PHP.

~ Deborah

Re: [Deborah] display '0' if field exists, but not approved

By Dave - February 27, 2011

Hi Deborah,

Try putting an if block below the foreach, like this:

<?php foreach ($commentsRecords3 as $record): // total number of approved comments ?>
<?php if ($record['status'] == "1"): ?>
<?php $recordsReturned = count ($commentsRecords3); ?>
<p class="subhd">comments (<?php echo ($commentsMetaData['totalRecords']); ?>)</p>
<?php endif ?>
<?php endforeach; ?>

<?php if (!$commentsRecords3): ?>
No Comments
<?php endif ?>


Let me know if that works for you. Thanks! :)
Dave Edis - Senior Developer
interactivetools.com

Re: [Deborah] display '0' if field exists, but not approved

By Dave - February 28, 2011

Glad to help! :)
Dave Edis - Senior Developer
interactivetools.com