p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

php_databases thread: can't change variable font color according to it's value


Message #1 by "Nikolopoulos Athanasios" <istion@o...> on Wed, 8 Jan 2003 10:30:14
> the $variable represents the result of a mysql query and if 
> it's value is, 
> let's say superior to 5, I would like to turn the value 
> string into red, 
> for example. The value must be insered into an html table but 
> not in a 

The best way to do things like this is to use arrays.

For example if we populate the array $my_colors with five 
possible colors like so..

	$my_colors = array ( 
					1 => "ffffff", 
					2 => "dddddd", 
					3 => "999999", 
					4 => "555555", 
					5 => "000000" 
				  ); 


Then to print out it would be like so..

<?php

	// Define colours..
	$my_colors = array ( 
					1 => "ffffff", 
					2 => "dddddd", 
					3 => "999999",
					4 => "555555",
					5 => "000000"
				  ); 

	// Select a colour to print..
	$color = 1;

	// Print out the html
	print "<font color=$my_colors[$color]>Hello World!</font>";

?>

To work this into your database query, simply 
set the value of $color by your query. As long as 
the value is 1,2, 3, 4 or 5 it will work.

If you need more colors extend the $my_colors array to add more colors
values.

Hope this helps,

Cheers,
Justin.



  Return to Index