Jump to content

Query with colou change.


cpharry

Recommended Posts

Hi,

 

I have got everything working except one thing. When the query that i run returns with a H i want it to change the echoed H's colour to red and the other option which is an A if that is returned echo it green.

 

	<?php 
$ally = str_replace("H","<p color= #FF0000 >H</p>",$row['ally']);
$ally = str_replace("A","<p color='green'>A</p>",$row['ally']);
echo $ally;
?>

This is what i have alread but it isnt working.

 

Any help appriciated.

Link to comment
https://forums.phpfreaks.com/topic/155637-query-with-colou-change/
Share on other sites

   $ally = str_replace("H","<p color= #FF0000 >H</p>",$row['ally']);
   $ally = str_replace("A","<p color='green'>A</p>",$ally);

 

That should work, if it is going to be either an H or an A you can do this:

 

if (strstr($row['ally'], 'A') !== false) {
    $color = "green";
    $letter = "A";
}else {
    $color = "#FF0000";
    $letter = "H";
}
$ally = str_replace($letter, "<p color='{$color}'>{$letter}</p>", $row['ally']);
echo $ally;

 

You could even do an array setup:

$letterColor = array("A" => "green", "H" => "#FF0000");
$letter = (strstr($row['ally'], "A") !== false)?"A":"H";

$ally = str_replace($letter, "<p color='{$letterColor[$letter]}'>{$letter}</p>", $row['ally']);

echo $ally;

 

Or if you want both to be replaced regardless.

$replace = array("A", "H");
$replaceWith = array("<p color='green'>A</p>", "<p color='#FF0000'>H</p>");

$ally = str_replace($replace, $replaceWith, $row['ally']);
echo $ally;

 

Hopefully one of those answers your question :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.