Displaying different image with a condition

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lukpa
Forum Newbie
Posts: 4
Joined: Fri Apr 24, 2009 12:22 pm

Displaying different image with a condition

Post 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>
Last edited by Benjamin on Fri Apr 24, 2009 8:44 pm, edited 1 time in total.
Reason: Changed code type from text to php
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Displaying different image with a condition

Post by McInfo »

Do you have a question, or are you just sharing your code?

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 11:25 am, edited 1 time in total.
lukpa
Forum Newbie
Posts: 4
Joined: Fri Apr 24, 2009 12:22 pm

Re: Displaying different image with a condition

Post 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
lukpa
Forum Newbie
Posts: 4
Joined: Fri Apr 24, 2009 12:22 pm

Re: Displaying different image with a condition

Post by lukpa »

it worked as a charm...Thank you guys!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Displaying different image with a condition

Post 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
}
 
lukpa
Forum Newbie
Posts: 4
Joined: Fri Apr 24, 2009 12:22 pm

Re: Displaying different image with a condition

Post by lukpa »

Much more elegant. Thank you John
Post Reply