Avatar of DS928
DS928
Flag for United States of America asked on

Displaying Image Based On Field Value

I am trying to display an image based on the value of a field.  I am getting this error.

Warning: Cannot modify header information - headers already sent by (output started at /home/content/d/s/t/dstr3/html/MENUHEAD/Pages/restpage.php:1) in /home/content/d/s/t/dstr3/html/MENUHEAD/Pages/restpage.php on line 427

<?PHP  
If ($Rating == ".5") 
	{
	header('Content-Type: image/x-png');
	readfile('http://www.menuhead.net/Images/Icons/Half.png');
	}
elseif ($Rating == "1")
	{
	header('Content-Type: image/x-png');
	readfile('http://www.menuhead.net/Images/Icons/1Head.png');
	}
elseif ($Rating == "1.5") 
	{
 	header('Content-Type: image/x-png');
	readfile('http://www.menuhead.net/Images/Icons/1andHalf.png');
	}
elseif ($Rating == "2")
	{
 	header('Content-Type: image/x-png');
	readfile('http://www.menuhead.net/Images/Icons/2Heads.png');
	}
elseif ($Rating == "2.5")
	{
 	header('Content-Type: image/x-png');
	readfile('http://www.menuhead.net/Images/Icons/2andHalf.png');
	}
elseif ($Rating == "3")
	{
 	header('Content-Type: image/x-png');
	readfile('http://www.menuhead.net/Images/Icons/3Heads.png');
	}
elseif ($Rating == "3.5")
	{
 	header('Content-Type: image/x-png');
	readfile('http://www.menuhead.net/Images/Icons/3andHalf.png');
	}
elseif ($Rating == "4")
	{
 	header('Content-Type: image/x-png');
	readfile('http://www.menuhead.net/Images/Icons/4Heads.png');
	}
elseif ($Rating == "4.5")
	{
 	header('Content-Type: image/x-png');
	readfile('http://www.menuhead.net/Images/Icons/4andHalf.png');
	}
elseif ($Rating == "5")
	{
 	header('Content-Type: image/x-png');
	readfile('http://www.menuhead.net/Images/Icons/5Heads.png');
	}
?>

Open in new window

PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
DS928

ASKER
This works.  But would a switch be better?

<?PHP  
If ($Rating == ".5") 
	{
	print ("<IMG SRC =http://www.menuhead.net/Images/Icons/RedHalfHead.png>");
	}
elseif ($Rating == "1")
	{
	print ("<IMG SRC =http://www.menuhead.net/Images/Icons/Red1Head.png>");
	}
elseif ($Rating == "1.5") 
	{
 	print ("<IMG SRC =http://www.menuhead.net/Images/Icons/Red1HalfHead.png>");
	}
elseif ($Rating == "2")
	{
 	print ("<IMG SRC =http://www.menuhead.net/Images/Icons/Red2Heads.png>");
	}
elseif ($Rating == "2.5")
	{
 print ("<IMG SRC=http://www.menuhead.net/Images/Icons/Red2HalfHeads.png>");
	}
elseif ($Rating == "3")
	{
 	print ("<IMG SRC =http://www.menuhead.net/Images/Icons/Red3Heads.png>");
	}
elseif ($Rating == "3.5")
	{
 	print ("<IMG SRC =http://www.menuhead.net/Images/Icons/Red3HalfHeads.png>");
	}
elseif ($Rating == "4")
	{
 	print ("<IMG SRC =http://www.menuhead.net/Images/Icons/Red4Heads.png>");
	}
elseif ($Rating == "4.5")
	{
 	print ("<IMG SRC =http://www.menuhead.net/Images/Icons/Red4HalfHeads.png>");
	}
elseif ($Rating == "5")
	{
 	print ("<IMG SRC =http://www.menuhead.net/Images/Icons/Red5Heads.png>");
	}
?>

Open in new window

COBOLdinosaur

Probably a switch would be better, but why not just have an array containing the strings?

You could set the index to the array with:
$idx=$rating/.5-1;
And go directly to the array to get the image.

Cd&
DS928

ASKER
Could you show ne more?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question