Jump to content

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 :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.