Page 1 of 1

Displaying different image with a condition

Posted: Fri Apr 24, 2009 12:27 pm
by lukpa
The idea is to use the result from a field and depending of the value of it to display different image:

Code: Select all

<td><div align="center"><span class="cifrarate">
                                        <?=GetValue($recSRC,"CredRate"); //[I get the value of this from the database, the result is a letter, but I want to be a graphic image of that letter]
 
 
$CredRate = GetValue($recSRC,"CredRate");
//$color = ("CredRate"); //Define the variable $color 
 
if ($CredRate = A) //If the color is red... 
{//do this 
echo "<img src=\"images/a.jpg\">"; 
} else if ($CredRate = B) {//however if the color is green, do this 
echo "<img src=\"images/b.jpg\">"; 
} else if ($CredRate = C) {//And if the color is blue, do this 
echo "<img src=\"c.jpg\">";
} else if ($CredRate = D) {//And if the color is blue, do this 
echo "<img src=\"images/d.jpg\">";
} else if ($CredRate = F) {//And if the color is blue, do this 
echo "<img src=\"images/f.jpg\">";
} else {//And finally, if color matches none of the conditions, display this 
echo " TOO NEW (No rating)"; 
}//end the statements 
?> 
</span></strong></div></td>

Re: Displaying different image with a condition

Posted: Fri Apr 24, 2009 2:43 pm
by McInfo
Do you have a question, or are you just sharing your code?

Edit: This post was recovered from search engine cache.

Re: Displaying different image with a condition

Posted: Fri Apr 24, 2009 2:54 pm
by lukpa
yeah, the question is, how it should be done..becuase the code I have does not work. I thought you are going to notice :D .
So yeah..PLEASE HELP

Re: Displaying different image with a condition

Posted: Fri Apr 24, 2009 2:57 pm
by John Cartwright

Re: Displaying different image with a condition

Posted: Fri Apr 24, 2009 3:27 pm
by lukpa
it worked as a charm...Thank you guys!

Re: Displaying different image with a condition

Posted: Fri Apr 24, 2009 3:36 pm
by John Cartwright
2nd pass..

You should also be quoting your strings. You probably arn't seeing the notices becauase your error level is set too low. I would always recommend developing with atleast

Code: Select all

error_reporting(E_ALL);
Secondly, you could simplify your script greaty.

Code: Select all

$range = range('a', 'f'); //store array of possible scores
$rating = strtolower(GetValue($recSRC,"CredRate")); //get value as lowercase
 
if (in_array($rating, $range)) {
   echo '<img src="images/'. $rating .'.jpg">'; 
} else {
   //invalid rating
}
 

Re: Displaying different image with a condition

Posted: Fri Apr 24, 2009 4:28 pm
by lukpa
Much more elegant. Thank you John