Page 1 of 1

Using an if statement to check for blank database entries

Posted: Fri Sep 26, 2003 5:44 pm
by wmowat
Hello,

I have got my page working for the most part now, but I'm trying to add an if statement.

What I want it to do is check to see if there is anything in the field, if there isn't I want it to print "Not Rated". This is the code I have so far.

MRating and WRating are the names of a field in a database, they should have a rating out of 10. eg. 2/10 4/10 etc. but if they are empty, I want the words "Not Rated" to be put in their place. Can anyone tell me what is wrong with this code?

Code: Select all

$Mrating = $lineї'MRating'];
$Wrating = $lineї'WRating'];
	   			
if (empty($Mrating)) 
{then $Mrating = "Not Rated";}

if (empty($Wrating)) 
{then $Wrating = "Not Rated";}
Thanks again,

Will Mowat
wmowat@nearnorthweb.com

Posted: Fri Sep 26, 2003 5:56 pm
by microthick
You could instead do this I think:

Code: Select all

$Mrating = $lineї'MRating'];
$Wrating = $lineї'WRating'];
               
if (empty($Mrating))
{$Mrating = "Not Rated";}

if (empty($Wrating))
{$Wrating = "Not Rated";}
Or even:

Code: Select all

if (empty($lineї'MRating']) {
$Mrating = "Not Rated";
} else {
$Mrating = $lineї'MRating'];
}

Posted: Fri Sep 26, 2003 6:09 pm
by wmowat
Sill not working, I get a parse error no matter what I try and do. I had it kind of working before, but it would put Not Rated for all of them instead of just the empty ones, also I think my database sets all the ratings fields to "Null" as a default value. I tried plugging that in in place of "" but nothing happened. Any more ideas?

Posted: Fri Sep 26, 2003 6:10 pm
by wmowat
Never mind, I got it working. Thanks...your code helped me get it going. I did it based on your 2nd example

Code: Select all

if ($lineї'MRating'] == "") { 
$Mrating = "Not Rated"; 
} else { 
$Mrating = $lineї'MRating']; 
}

if ($lineї'WRating'] == "") { 
$Wrating = "Not Rated"; 
} else { 
$Wrating = $lineї'WRating']; 
}
That is the working code

Posted: Fri Sep 26, 2003 6:12 pm
by microthick
What does the parse error say?

And can you post your loop construct to loop through the query results?

Posted: Fri Sep 26, 2003 6:12 pm
by Unipus
How about:

Code: Select all

$Mrating = $line["MRating"]; 
$Wrating = $line["WRating"]; 
                
if (!$Mrating) 
{$Mrating = "Not Rated";} 

if (!$Wrating) 
{$Wrating = "Not Rated";}
If that doesn't work, what exactly is the error you're getting? Have you tested the output of $Mrating before and after you re-assign it? Have you checked your query to make sure you're getting the right values in the first place?