0

I'm trying to make a simple if else statement but I cannot get the desired results and am obviously using the wrong methods.

This is what I have:

if (!$res7 and !$res8) {output one - this should appear if both $res7 and $res8 are empty}

else if (!$res8)  {output two - this should appear if $res8 is empty}

else if (!$res7) {output three - this should appear if $res7 is empty}

The outputs show what I desire, can someone help with fixing it? Thanks

EDIT the full code: $res7 and $res8 are results from a MySQL query

$res8 = $pdo->query($query8);

if (empty($res7) && empty($res8)) {

printf("We currently have no reviews for this park or its attractions. If you have been to %s please take a minute to give your feedback. You don't need to register to leave reviews or ratings. Thank you." . PHP_EOL, $row[name]);
}

elseif (empty($res8)) {

printf("We currently have no attraction reviews for this park. If you have been to %s please take a minute to give your feedback. You don't need to register to leave reviews or ratings. Thank you." . PHP_EOL, $row[name]);

}

elseif (empty($res7)) {

printf("<h3>Park Review</h3>We currently have no reviews for this park as a whole. If you have been to %s please take a minute to give your feedback. You don't need to register to leave reviews or ratings. Thank you." . PHP_EOL, $row[name]);
}

else { 
printf("all good");
}
14
  • 1
    What are the values for $res7 and $res8? What's happening that shouldn't be? Commented Aug 20, 2013 at 17:06
  • 1
    What? PHP doesn't have the and keyword. Commented Aug 20, 2013 at 17:07
  • Try using the empty() method instead of ! Commented Aug 20, 2013 at 17:07
  • 5
    it most certainly does... php.net/manual/en/language.operators.logical.php Commented Aug 20, 2013 at 17:08
  • 1
    @user2574794 - You can just replace your if statements with if (($res7->rowCount() == 0) && ($res8->rowCount() == 0)) and so on. Commented Aug 20, 2013 at 17:39

2 Answers 2

3

This is how you're initialising $res8

$res8 = $pdo->query($query8);

At this point, $res8 is a result set, containing the results of your query. What you're trying to do is check to see if that resultset is empty. You can't call empty() on it, because even with no results in it, it's still a resultset, so it's returning FALSE on your call.

What you need to do is using the in-built functions to see how many rows it contains:

if ($res8->rowCount() == 0) {
    ....
Sign up to request clarification or add additional context in comments.

Comments

1

If you mean empty as empty and not as false, then do it this way:

if (empty($res7) && empty($res8)) {
    // First case
} elseif (empty($res8)) {
    // Second case
} elseif (empty($res7)) {
    // Third case
} else {
    // Not $res7 nor $res8 are empty
}

6 Comments

@3ventic Yes, I've confused it with is_null for some reason.
@Campari: Oh, how I love how consistent PHP is :-P
@RocketHazmat On the "bright side", it forces you to look at the manual more often than you normally would :)
@campari thanks for the reply, I have copied it exactly and even when $res7 or $res8 has data it is still going to the final else output. I should say $res7/8 are results directly from a MySQL query, would that effect the code needed?
@user2574794 - can you edit your question to include your actual code?
|

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.