Link to home
Start Free TrialLog in
Avatar of DS928
DS928Flag 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

Avatar of DS928
DS928
Flag of United States of America image

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

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&
Avatar of DS928

ASKER

Could you show ne more?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial