I have a blog with comments and it uses a MySQL database. I recently built in a comment moderation system and it is fully functional and working as I want it. However I feel like a part of my code is more complicated than it needs to be. Here is the code (just the peice in question) and then I'll explain:
Code: Select all
<a href="../journalcomment.php?id='.$row['id'].'">';
if ($row['commentcount'] < 1) {
echo '0 Comments';
} elseif ($row['commentcount'] == 1 && $row['approved']<>0) {
echo '1 Comment';
} elseif ($row['commentcount'] == 1 && $row['approved']==0) {
echo '0 Comments';
} elseif ($row['commentcount'] > 1 && $row['approved']<>0) {
echo $row['commentcount'] . ' Comments';
} elseif ($row['commentcount'] > 1 && $row['approved']==0) {
echo '0 Comments';
}
echo '
</a><p></td>
Like I said, this is functional and working I just want to clean up my if statements. I am checking to see if there is a comment, and if so, are they/it approved? I want it to say "0 Comments", "1 Comment" or "X Comments" appropriatley. I am checking both the conditions with success but it feels repetitive. Should I just be happy with my working code and walk away, or is it worth rewriting? If so, how would you do it? I tried to keep the code I present here brief and I think that's all you need. Suffice to say I am counting the comments correctly and displaying them correctly on a different page. This is just to create the link that leads to that page. I can provide more code if necessary.
Thanks!
Craig