mercredi 6 mai 2015

Display the count of an item and its parent using PHP

Need help displaying the correct count of an item and its parent on the first item added.

I am trying to display the total count of all comments (users can reply to a comment so I need to count the parent message) It all works UNLESS the count is 1. If the count is 1 it displays 2 no matter what. (So if there is only one comment and no one has replyed to the comment and made another comment the number still shows 2) How would I go about fixing this? Do i need to add another if statement or an if else statement maybe? I am confusing myself so figured I would just submit a question. This is my first question I have ever asked on here. See my code below. Let me know if I need to include more information


<?php if (!empty($comments)) : ?>
   <?php foreach($comments as $pid => $parent_messages) : ?>
      <?php $comments_count = (count($comments)) + (count($parent_messages)); ?>
      <span class="badge pull-right"><span class="glyphicon glyphicon-comment"></span><?php ($comments_count != 0)  ? print ' '.$comments_count.' comments' : ''; ?></span>
   <?php endforeach;?>
<?php endif;?>

Aucun commentaire:

Enregistrer un commentaire