2013-08-20 3 views
0

나는 간단한 else if 문을 만들려고하지만 원하는 결과를 얻지 못하고 잘못된 방법을 사용하고 있습니다.로직이 작동하지 않는 경우 간단한 PHP

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} 

출력은 누군가가 그것을 해결에 도움이 될 수 있습니다, 내가 원하는 것을 보여

이 내가 가진 무엇인가? 감사합니다

EDIT 전체 코드 : $의 res7 및 $ MySQL의 쿼리의 결과가 res8 있습니다

$res8 = $pdo->query($query8); 당신이 false로, 다음이 방법을 빈 empty로하지 의미하는 경우

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"); 
} 
+1

$ res7와 $ res8의 값이 무엇입니까? 해서는 안되는 일이 뭐야? – andrewsi

+1

무엇? PHP에는'and' 키워드가 없습니다. – Brian

+0

! 대신 empty() 메소드를 사용해보십시오! –

답변

3

입니다
$res8 = $pdo->query($query8); 

이 시점에서 $res8은 쿼리 결과를 포함하는 결과 집합입니다. 당신이하려고하는 것은 결과 집합이 비어 있는지 확인하는 것입니다. 결과가 없더라도 여전히 결과 집합이므로 호출시 FALSE가 반환되므로 empty()에 전화 할 수 없습니다. 당신이 포함 된 행 수를 확인하기 위해 내장 기능을 사용 할 필요가 무엇

:

if ($res8->rowCount() == 0) { 
    .... 
1

: 당신이 $res8

을 초기화하는있는 방법
if (empty($res7) && empty($res8)) { 
    // First case 
} elseif (empty($res8)) { 
    // Second case 
} elseif (empty($res7)) { 
    // Third case 
} else { 
    // Not $res7 nor $res8 are empty 
} 
+1

['empty()']'is_empty()가 아닙니다. – 3ventic

+0

@ 3ventic 예, 저는 이것을'is_null'과 혼동했습니다. 몇몇 이유. – federicot

+1

@Campari : 오, 어떻게 일관성있는 PHP가 좋을지 :-P –

관련 문제