Forum Moderators: coopster

Message Too Old, No Replies

If Statement working, but else statement displaying anyway.

I kind of get the feeling that this is easier than I think.

         

Gahein

7:46 pm on Mar 18, 2009 (gmt 0)

10+ Year Member



I have a profile page set-up for a website with 4 members. The page should display a featured Image from that person, but only if that person has an image in their gallery. The Media table stores all the images, and the MemberID row allows me to sort the table by user, to see if the person has anything their gallery. The Members table tells me which user is which, and any information about that user. So the php I have is this:

$hrow = mysql_fetch_array(mysql_query("SELECT * FROM Members WHERE MemberID='$hID'"));
$f = $hrow['FeatureIMG'];

$nullrow = mysql_fetch_array(mysql_query("SELECT * FROM Media WHERE MemberID='$hID'"));

if (is_null($nullrow['MediaID']) !== false)
echo "This space is null, and thus, does not show the else statement!";
else
$irow = mysql_fetch_array(mysql_query("SELECT * FROM Media WHERE MediaID='$f'"));
$t = $irow['Title'];
echo "<h3 class=\"heroes\" id=\"title\">";
echo "Featured Image: <a href=\"/gallery/sngl_img.php?MediaID=" . $f . "\">" . $t . "</a>";
echo "</h3>";
echo "<div class=\"img-holder\" align=\"center\">";
echo "<img class=\"the-image\" src=\"http://mysite.com/largeimages/" . $f . "_lg.jpg\" />";
echo "</div>";

But for some reason, when I enter this, It displays both the if command (This space is null, and thus, does not show the else statement!) and then immediatly afterward, it shows the else statement. So I know both are getting through, but why is it displaying the else statement?

rob7591

8:23 pm on Mar 18, 2009 (gmt 0)

10+ Year Member



Use { } around your statements that contain more than one line. Otherwise, how does the if statement know where to start parsing code again?

if () {
statements;
on;
more;
than;
one;
line;
} else {
continue;
more;
statements;
}

blang

8:32 pm on Mar 18, 2009 (gmt 0)

10+ Year Member



First and foremost, you should be performing a JOIN on those tables based on a related key (the `MemberID` field for example).

Does your table actually store NULL values, or are they empty strings?

inveni0

8:48 pm on Mar 18, 2009 (gmt 0)

10+ Year Member



rob7591 is right.

Gahein

10:36 pm on Mar 18, 2009 (gmt 0)

10+ Year Member



Duh. I don't know why I didn't think that. I guess I thought {} were only used on while statements....

thanks! it fixed it right up.

as for the JOIN thing. I don't quite know what that is, but I'll do some research, sounds like it'd save me time. This is my first php website, so I'm mostly just using whatever code works, and not necessarily the best code for the job.

And, as for these specific values, they're empty strings, not NULL values.

Thanks so much for the quick reply!

coopster

11:00 am on Mar 19, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The PHP manual page regarding Control Structures [php.net] will show you syntax and examples for all kinds.

BTW, welcome to WebmasterWorld, Gahein.