Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.