Jump to content

can't echo out else statement


contra10

Recommended Posts

I'm trying to echo out a statement if somethin gis untrue but its not working

<?php
$queryear = "SELECT * FROM `ratings` WHERE `songtitle` = '$name'";
$resultear = mysql_query($queryear) or die(mysql_error());
while ($postedear = mysql_fetch_assoc($resultear))
{
$avsong = "{$postedear['songtitle']}";
$linksong = "{$postedear['song']}";
$ratedavg = "{$postedear['average']}";

if ($ratedavg > 1){
echo"<td>$ratedavg / 10</td></tr>";
}else{
echo "<td>No Rating</td></tr>";
}
}
?>

THe average shows for those that have been rated but the other statement is suppose to show for those that havn't but it doesn't[/code]

Link to comment
https://forums.phpfreaks.com/topic/173818-cant-echo-out-else-statement/
Share on other sites

i see what your saying

in this case one of my $ratedavg = 5

 

5 > 1 so it would show the result but if there is no rating and the $ratedavg is equal to nothing shouldn't it show. Or would I have to put something like

 

<?php
if ($ratedavg == true){
echo"<td>$ratedavg / 10</td></tr>";
}else{
echo "<td>No Rating</td></tr>";
}
?>

 

I think it would cast the string as an int in that comparison, and all strings get cast to 0 if i'm not mistaken. So do this

$ratedavg = $postedear['average'];

 

and see if it helps. There is no reason to surround the data with quotes anyways, as if its a string it will be treated as such when assigned, and if its an int it will be treated as such, but forcing it to be a string always can run into problems

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.