PHP: Change color of text based on $value
I found this plugin:
http://www.jnathanson.com/blog/client/jquery/heatcolor/index.cfm#examples
CSS/PHP: Change font color based on value retrieve from MySQL
This will work
<?php
$status=$row['Book_Status'];
if($status=="Approved")
{
$color="color:green";
}
else if($status=="Pending")
{
$color="color:yellow";
}
else
{
$color="color:red";
}
echo "<table><tr><td></td><td></td><td></td><td></td><td></td><td style='$color'>".$status ."</td></tr></table>";
?>
How to change color of text based on $value
Store your calculation in a variable and then add a condition to your style to change colour vased on value of you variable. (e.g.:
<?php $value = round(100 * ($data[$tabele[$i]][$keys[$tabele[$i]][0]] + $data[$tabele[$i]]["RESERVED"] + $data[$tabele[$i]]["BIZ_AVAILABLE"]) / ArraySum($data[$tabele[$i]]) ,2)); ?>
<td style="font-weight: bold; color: <?php if($value<10):?>red<?php else:?>black<?php endif;?>;"><?php echo $value?>%</td>
How can i change the color of my text if it equals a given value in PHP
You need to use echo
to display HTML inside PHP tags.
The font
tag is not supported in HTML5. You're better off using p
and changing its colour using CSS.
From your question, it seems like you're trying to loop through an array. It can be done like this:
<?php
foreach($arrayValues as $arrayValue) {
$Danger = $arrayValue[2];
if($Danger == "High") {
echo'<p style="color: red;">'.$Danger.'</p>';
}else if($Danger == "Medium") {
echo'<p style="color: green;">'.$Danger.'</p>';
}
}
?>
PHP: Change color of text based on $i
Put the color script under counting script and change $v
to $i
<div id="body">
<ul>
<li>Folder 1:<strong>
<?php
// integer starts at 0 before counting
$i = 0;
$dir = 'folder1/images/';
if ($handle = opendir($dir)) {
while (($file = readdir($handle)) !== false){
if (!in_array($file, array('.', '..')) && !is_dir($dir.$file))
$i++;
}
}
$color = "#000000";
if (($i >= 0) && ($i <= 9))
$color = "#E54028";
else if (($i >= 9) && ($i <= 15))
$color = "#F18D05";
else if ($i >= 15)
$color = "#61AE24";
// prints out how many were in the directory
echo "<span style=\"color: $color\">$i items</span>";
?>
</strong></li>
</ul>
</div>
You might want to adjust the ranges for $i
Change font colour inside php echo statement depending on variable value
Try this:
<td><span style="font-family:Arial, Helvetica, sans-serif;<?php if($variable<0)echo "color:red"?>"><?php echo $variable; ?></span></td>
Note that the <font>
tag was deprecated a long time ago so I replaced it with a <span>
tag.
PHP setcookie function to remember the selected BackGround and Text Color
there are quite a few issues in the above code.You need to echo the colours, you should use the style
declaration for the body tag (would be better as css!) and you ought to test that the various form elements are set in the POST array. Perhaps this might be of interest?!
<?php
if( isset( $_POST["chk"], $_POST['NewBGColor'], $_POST['NewTextColor'] ) ) {
setcookie( "BColor", $_POST['NewBGColor'], time()+3600 );
setcookie( "TColor", $_POST['NewTextColor'], time()+3600 );
}
$Bcolor = isset( $_COOKIE['BColor'] ) ? $_COOKIE['BColor'] : 'white';
$TxtColor = isset( $_COOKIE['TColor'] ) ? $_COOKIE['TColor'] : 'black';
?>
<html>
<head>
<title>Cookie</title>
</head>
<body style="background:<?php echo $Bcolor;?>;color:<?php echo $TxtColor;?>">
<form method="Post">
Select A New BG Color:
<Select name="NewBGColor" >
<option value="WHITE">WHITE
<option value="BLACK">BLACK
<option value="RED">RED
<option value="BLUE">BLUE
</Select>
Select A New Text color:
<Select name="NewTextColor" >
<option value="WHITE">WHITE
<option value="BLACK">BLACK
<option value="RED">RED
<option value="BLUE">BLUE
</Select>
<input type="hidden" name="chk" value="true"/>
<input type="submit" value="submit" />
</form>
</body>
</html>
Or a slight modification that uses PHP to generate the HTML and remember
the chosen values
<?php
if( isset( $_POST["chk"], $_POST['NewBGColor'], $_POST['NewTextColor'] ) ) {
setcookie( "BColor", $_POST['NewBGColor'], time()+3600 );
setcookie( "TColor", $_POST['NewTextColor'], time()+3600 );
}
$Bcolor = isset( $_COOKIE['BColor'] ) ? $_COOKIE['BColor'] : 'white';
$TxtColor = isset( $_COOKIE['TColor'] ) ? $_COOKIE['TColor'] : 'black';
$colours=array(
'white','red','blue','black'
);
?>
<html>
<head>
<title>Cookie</title>
</head>
<body style="background:<?php echo $Bcolor;?>;color:<?php echo $TxtColor;?>">
<form method="Post">
Select A New BG Color:
<select name="NewBGColor" >
<?php
foreach( $colours as $colour ){
$selected=$colour==$Bcolor ? ' selected' : '';
printf('<option value="%1$s"%2$s>%1$s',$colour,$selected);
}
?>
</select>
Select A New Text color:
<select name="NewTextColor" >
<?php
foreach( $colours as $colour ){
$selected=$colour==$TxtColor ? ' selected' : '';
printf('<option value="%1$s"%2$s>%1$s',$colour,$selected);
}
?>
</select>
<input type="hidden" name="chk" value="true"/>
<input type="submit" value="submit" />
</form>
</body>
</html>
Related Topics
How to Update Timezonedb in PHP (Updating Timezones Info)
Url Querystring with a PHP Include
SQL Server Error 1934 Occurs on Insert to Table with Computed Column PHP/Pdo
Crypt for Password Hashing. Blowfish Produces Weird Output
Converting a Byte Array into an Image Using PHP and HTML
How to Pass Parameters from Bash to PHP Script
What's the Best Way/Practice to Get the Extension of a Uploaded File in PHP
Single Result from Sum with MySQLi
PHP Twitter API - How to Pull in Multiple Users Tweets
Make Text Between Asterisks Bold
Laravel Collection Converts Array to Object
PHP Upload Progress in PHP 5.4 Is Not Working. Session Variables Not Set
Adding Additional Persist Calls to Preupdate Call in Symfony 2.1
How to Sftp Upload Files from PHP