Forum Moderators: coopster

Message Too Old, No Replies

Help creating IF statement - using EMPTY

empty($row) - I'm using a database

         

applealps

1:28 pm on Oct 11, 2005 (gmt 0)

10+ Year Member



I'm trying to get this function to return None if the select query matches nothing. When I do,!empty, it works, but with just empty nothing happens.


function film_menu ($yes_other, $no_other, $menu_head, $film) {

$query = "SELECT * FROM items WHERE $yes_other $no_other ORDER BY date DESC LIMIT 4";
$result = mysql_query($query);

echo "$menu_head<br><span class=\"head\">$film</span><br>&nbsp;<br>";

while($row = mysql_fetch_assoc($result)) {

[b]if(empty($row)){
return None;
}[/b]
else if(empty($row['theme02'])) {
$themes = "{$row['theme01']}";
}
else {
$themes = "{$row['theme01']} {$row['theme02']}";
}
echo "
<div class=\"filminfo\"><a href=\"film_film.php?p={$row['link']}\";>{$row['title']}</a><br>
$themes<br>{$row['rating']} - {$row['length']}wds - <a href=\"reviews/view.php?id={$row['item_id']}\">reviews</a><br>{$row['summary']}</div>";
}

}

I've tried empty($query) and empty($result) neither work but both work when used with,!empty. What's happening!? I've moved the If statement around in case it's in the wrong place, but the thing just won't work.

I give up!

arran

1:55 pm on Oct 11, 2005 (gmt 0)

10+ Year Member



while($row = mysql_fetch_assoc($result)) { 
// At this point $row will never be empty (per your while statement above)
// Consequently, the if statement below is redundant.
if(empty($row)){
return None;
}

arran.

jatar_k

3:01 pm on Oct 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



why not use mysql_num_rows [php.net] after you do the mysql_query call.

then you could test the number of returned rows before you get to your while loop. You could do something like

$numrows = mysql_num_rows($result);
if($numrows < 1){
return None;
}
while($row = mysql_fetch_assoc($result)) {

applealps

3:30 pm on Oct 11, 2005 (gmt 0)

10+ Year Member



It works!

jatar_k, I can't tell you how much I thank you. :D The problem was giving me such a headache!

This is the first function I've ever written, last night I was so happy when it all worked, :D , but my euphoria was short lived. Just when you get over one php hill, there's a bigger one on the horizon. lol

jatar_k

3:32 pm on Oct 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well we try to make the hills a little smaller or at least give you a push up them ;)

glad it worked