Log in

View Full Version : Resolved help with 'elseif' statement



liamallan
04-26-2010, 02:33 PM
i been trying for the first time to insert an 'elseif' statement, but having trouble.

this is my code:

$getRequests = mysql_query("SELECT * FROM `friend_requests` WHERE `username` = \"".$req_user_info['username']."\" AND `by` = \"".$session->username."\"");//gets requests
$getFriends = mysql_query("SELECT * FROM `friends` WHERE `friendname` = \"".$req_user_info['username']."\" AND `username` = \"".$session->username."\"");//gets friends
if (mysql_num_rows($getFriends) == 0){ //checks if user isnt a friend
echo "<a href=\"friendrequest.php?user=".$req_user_info['username']."\" title=\"Become Friends With ".$req_user_info['username']."\"><img src=\"friends_icon.png\" width=\"25\" height=\"25\" border=\"0\" /></a><br>";
}
elseif(mysql_num_rows($getRequests) => 0){ //checks for existing requests
echo "request pending";
}
else{
echo ( "<br>[<font color='#666666'>You are friends</font>]<br>");
}
all i get is a blank error screen, any ideas? thanx

djr33
04-26-2010, 05:31 PM
There are a few problems:

1. => is a different operator (assigning a relationship between variables like in a foreach() loop). You want to use >=.
2. In this case, I don't see why you'd want "greater than or equal to" and not just "greater than" because you already have an if for when it is 0. So I'd just use >0.
3. You should turn on error reporting in php.ini or set it manually when you're debugging. Google "php error reporting" or look at the various methods on php.net. It's a bit hard to find the information and even harder to explain it here, because there are many ways to do it and it depends on what kind of access you have to the server. But however you do it, use error reporting! Blank error screens are difficult to work with.

Beyond that, I don't see anything wrong.

Remember that elseif is an unusual construction because it removes complexity from the layers of code:
//regular if and else statements:
if (...) {
...;
}
else {
if (...) {
...;
}
else {
if (...) {
...;
}
else {
...........
}
}
}


///using elseif or else if:
if (...) {
...;
}
elseif (...) {
...;
}
elseif (...) {
...;
}
else {
........
}