0

I cannot get my PHP script to echo the second else statement if the first result empty.

The way my script currently works is "Print Addresses (from another array) > List Comments", however even if a comment is empty for an address is will either print the comments or nothing, I cannot make the script echo the word "No comment".

if(!empty($row['id']))
        {
        echo "$row[comment]<br/>";
        }
        else
        {
            echo "no comment<br/>";
        }

Any help appreciated. Thanks.

1
  • Try var_dump($row) to see exactly what your array contains. Match it with the type comparison tables to see what's going on. Commented Jan 2, 2012 at 22:47

1 Answer 1

4

Assuming this comes from a database and each row has an id, this will always be true:

if(!empty($row['id']))

Try:

if(!empty($row['comment']))

If id is something else, the same logic applies: check the value that you intend to print:

if (!empty($row['id']) && !empty($row['comment']))
{
    echo $row['comment'].'<br/>';
}
else
{
    echo "no comment<br/>";
}

EDIT: If this code is looping through all comments attached to a post or something, there will never be any output if there are no comments to loop through. In that case try something like this:

if (count($comments) === 0)
{
    echo "no comments<br />";
}
else
{
    foreach ($comments as $row)
    {
        if (!empty($row['comment']))
        {
            echo $row['comment'].'<br />';
        }
        else
        {
            echo "no comment<br />";
        }
    }
}

OR:

$comment_count = 0;

foreach ($comments as $row)
{
    if (!empty($row['comment']))
    {
        echo $row['comment'].'<br />';
        $comment_count++; // We have at least one comment
    }
    else
    {
        echo "no comment<br />";
    }
}

if ($comment_count === 0) echo 'no comments<br />';
Sign up to request clarification or add additional context in comments.

4 Comments

Hello, I tried this, however it still echoes the same as before. To give you a better idea of what I am trying to do I have attached a screenshot link[/link] IT first goes through properties, and then displays their relevant comments. I just cannot for the life of me work out why it won't hide blank entries though. On the attached screenshot these appear as yellow lines across the page (where the css is supposed to wrap around the result). Thanks for your help thus far...
I took a guess at what your problem is, if this doesn't help you will need to add your complete releveant code to your post.
Well the comment prints without problem. The results are pulled from a database, and the comments are pulled (and listed). If the ID of the house matches the housed of the comment then the comment is listed (and printed in order) using the array.
I don't get what if(!empty($row['houseid'])) is supposed to do when your SQL query includes where houseid = ".$row['id'].". Did you try either method in my edit? I didn't see it in the pastie (which you should paste directly into your post here, use the code formatting tools). Try checking if (mysql_num_rows($result2) === 0) and then printing the "No comments" text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.