cpharry Posted April 25, 2009 Share Posted April 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/155637-query-with-colou-change/ Share on other sites More sharing options...
premiso Posted April 25, 2009 Share Posted April 25, 2009 $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 Quote Link to comment https://forums.phpfreaks.com/topic/155637-query-with-colou-change/#findComment-819172 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.