There must be a better way to check my logic in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
craigwg
Forum Newbie
Posts: 23
Joined: Tue Nov 03, 2009 7:48 pm

There must be a better way to check my logic in PHP

Post by craigwg »

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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: There must be a better way to check my logic in PHP

Post by McInfo »

Code: Select all

if ($row['commentcount'] < 1 || $row['approved'] == 0)  {
    echo '0 Comments';
} else {
    echo $row['commentcount'] . ' Comment';
    if ($row['commentcount'] > 1) {
        echo 's';
    }
}
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 4:58 pm, edited 1 time in total.
craigwg
Forum Newbie
Posts: 23
Joined: Tue Nov 03, 2009 7:48 pm

Re: There must be a better way to check my logic in PHP

Post by craigwg »

Brilliant! Thanks! Any tips for being better at formulating this stuff on my own in the future?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: There must be a better way to check my logic in PHP

Post by McInfo »

Practice.

Edit: This post was recovered from search engine cache.
Post Reply