Jump to content

Text color depending on data


Texan78
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hello, I am not good at explaining things but I will do my best. I have this small script that reads this text file then displays the data in a table. What I am trying to do is on one of the table cells the text that is displayed in I want the text to be a certain color depending on what it says. I.E. weak = yellow, moderate = orange and so on. I have this snippet of code I have used in the past to do this but for some reason when I plug the variable into the table the data doesn't display. 

 

This is the snipped that reads the text file to get the data and works perfect. 

 

//$tintensity[x] is the intensity of the storm


preg_match_all("/Intensity class (.*)=/Uis", $tracnopara, $temp);
$tintensity = $temp[1];

Then I display it in the table like this and it works fine. 

<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . strtolower($tintensity[$k]) . "</td>

Now this is how I am trying to assign the color to the text which as worked fine for me in the past. 

 

$stormIntensity = '';


           switch($tintensity)
{
              case 'weak':
                    $stormIntensity = 'rgb(255, 255, 0)';
                             break;
              case 'moderate':
                    $stormIntensity = 'rgb(255, 102, 0)';
                             break;
              case 'strong':
                    $stormIntensity = 'rgb(255, 0, 0)';
                             break;
              case 'severe':
                    $stormIntensity = 'rgb(217, 0, 126)';




}
 

But when I try to use it in the cell like this replacing the "$tintensity" that is normally there that displays the data then nothing shows. 

<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . strtolower($stormIntensity[$k]) . "</td>

I have tried it like this and no luck ether

<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>" . $stormIntensity[$k] . "</td>

What am I missing or doing wrong? It has been a few months since I have had to work with PHP so still trying to remember things. 

 

-Thanks

 

 

Link to comment
Share on other sites

I fail to see what the strtolower is supposed to accomplish.

 

Also - if you want to change the color you need to do it in the style but you are not.

 

Also - $stormintensity is a string;  $k is not defined (to us); what do you expect to get from "$stormintensity[$k]" ?

Link to comment
Share on other sites

I fail to see what the strtolower is supposed to accomplish.

 

Also - if you want to change the color you need to do it in the style but you are not.

 

Also - $stormintensity is a string;  $k is not defined (to us); what do you expect to get from "$stormintensity[$k]" ?

 

Heh, I failed to notice this. Good catch.

Link to comment
Share on other sites

Also - $stormintensity is a string;  $k is not defined (to us); what do you expect to get from "$stormintensity[$k]" ?

 

strtolower takes the data and makes it all lower case.

 

Ignore $stormintensity[$k] that is part of the loop. I posted it as an example  as that is the variable I use to populate that cell which works fine.  I think you maybe be looking at the wrong part of my code or I didn't explain it correctly and you're not understanding it. 

 

I did dawn on me after i posted that I need to be adding it to the style but it still isn't working.

<td style='background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em; color:" . $stormIntensity . "'>" . strtolower($tintensity[$k]) . "</td>

Here is an example. Under "Intensity" and "Trend" I want to change the text a certain color depending on what that text is. 

 

http://www.mesquiteweather.net/tracreportNS.php

 

If I need to post my entire code I can if that would be easier. I was just trying to streamline it and post only what was relevant. 

Link to comment
Share on other sites

Why are you posting code that doesn't pertain to what you want solved?  Sorry but I don't understand your last post at all.

 

IF YOU want a color effect on a td, you need to have something akin to:

 

...style='color:$color_value;' in your td statement.

 

AND why don't you just make the rest of your static style attributes part of a class and avoid having them repeated for each td element?  A much wiser and more efficient way of doing things.

Link to comment
Share on other sites

Why are you posting code that doesn't pertain to what you want solved? 

 

 

It DOES pertain but you are questioning something that WORKS and has nothing to do with what I am trying to do. I posted it as an example of what WORKS vs what DOESN'T vs what I have TRIED. 

 

strtolower($tintensity[$k]) is the variable used to populate the data in that cell. "strtolower" is to take the data from the text file and make it all lowercase. [$k] is the loop so each new data has it's own row in the table. I really didn't think I needed to go into detail explaining that as anyone with PHP experience would recognize that as such. 

 

Now when I take the variable  "$tintensity" and insert it into a switch like this.....

 

$stormIntensity = '';


           switch($tintensity)
{
              case 'weak':
                    $stormIntensity = 'rgb(255, 255, 0)';
                             break;
              case 'moderate':
                    $stormIntensity = 'rgb(255, 102, 0)';
                             break;
              case 'strong':
                    $stormIntensity = 'rgb(255, 0, 0)';
                             break;
              case 'severe':
                    $stormIntensity = 'rgb(217, 0, 126)';


}

That now creates a new variable called "$stormIntensity" which in theory should take that data depending on what it is and assign a color to it. 

 

BUT, when I replace "strtolower($tintensity[$k])" in the loop with the new variable from the switch "strtolower($stormIntensity[$k])" it doesn't work. 

 

I did realize AFTER I posted that it needs to be in the styling not in the actual loop. Which I have done here but, it still doesn't work. 

$intStyle = "background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em; color: ' . $stormIntensity . '";
AND why don't you just make the rest of your static style attributes part of a class and avoid having them repeated for each td element?  A much wiser and more efficient way of doing things.

 

 

 

I agree, and that is what I normally do AFTER I get the look I am going for first and get everything working correctly. Then I go back and clean it up as it is easier and faster for ME that way when I am first putting something together. That might not be what everyone does or recommends but that is what I like to do and it works for me. Everyone has their own way of doing things. 

 

I am currently still writing this script so I was posting raw code that I hadn't been cleaned up yet but, since posting it I have cleaned it up. That is irrelevant to my question though. 

 

As you can see, I have cleaned it up. Here is the entire code. 

<?php

// The TRACreportshort takes Nexstorm's full TRACreport and shortens it to a more
// managable size for displaying.
//
// It chops up the vital storm cell info, places it in arrays, and redisplays it as we wish
//

// Set some default values

$measure = "mi"; // km, nm, or mi depending on your Nexstorm measurements

// Let's assign the tables some styles
    $tracTable = "width:100%; margin-top: 15px; background-color:#000; color:#FFF;";
    $td1Style = "padding: 3px 0px 3px 0px; font-size: .9em; text-align: center; color: #00FF00;";
    $tr1Style = "background-color:#333; color:#3299cc; font-weight: normal; border: 1px #666 solid; text-align: center; font-size: .9em;";
    $tr2Style = "background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;";
    $intStyle = "background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em; color: ' . $stormIntensity . '";

// Load the TRACreport and remove carriage returns, change the word 'no change' to 'steady'
$trac = implode('', file('lightning/nexstorm/TRACReport.txt'));
$tracnopara = preg_replace( '|\n|', '=', $trac );
$tracnopara = preg_replace( '|No change|', 'Steady', $tracnopara );
$tracnopara = preg_replace( '|/minute|', '  a min', $tracnopara);

// Intialize values
$tracreport = "";
$tnumber = 0;
$alertflag = 0;
$tracwarning = "";

// If there aren't any thunderstorms, then say it

if (preg_match("/No thunderstorms detected/i", $trac)) {
		$tracreport = "<table style='{$tracTable}'>
			<tr>
			    <td style='{$td1Style}'>No nearby thunderstorms detected</td>
			</tr>
               </table>"; }
	else {

// If there are thunderstorms, process them, putting them into various arrays

		// $tnumber[x] is for number of thunderstorms

		preg_match("/Tracking (.*) thunderstorms/Uis", $tracnopara, $tnumber);

		// $tid[x] is the ID number of the storm

		preg_match_all("/Thunderstorm ID (.*) detected/Uis", $tracnopara, $temp);
		$tid = $temp[1];

		// $tdtime[x] is the time detected

		preg_match_all("/detected (.*)=/Uis", $tracnopara, $temp);
		$tdtime = $temp[1];

		// $tdirection[x] is the direction of the storm

		preg_match_all("/Storm location bearing (.*) dgr/Uis", $tracnopara, $temp);
		$tdirection = $temp[1];

		// $tdistance[x] is the distance in $measure amounts

		preg_match_all("/distance (.*) $measure/Uis", $tracnopara, $temp);
		$tdistance = $temp[1];

		//$tintensity[x] is the intensity of the storm

		preg_match_all("/Intensity class (.*)=/Uis", $tracnopara, $temp);
		$tintensity = $temp[1];

		//$ttrend[x] is the trend of the storm

		preg_match_all("/Intensity trend (.*)=/Uis", $tracnopara, $temp);
		$ttrend = $temp[1];

		//$tcurraterate[x] Current strikerate

		preg_match_all("/current strikerate (.*)=/Uis", $tracnopara, $temp);
		$tcurraterate = $temp[1];

		//$tpeakrate[x] Peak strikerate

		preg_match_all("/peak strikerate (.*)=/Uis", $tracnopara, $temp);
		$tpeakrate = $temp[1];

		//$tpeakrate[x] Total recorded strikes

		preg_match_all("/total recorded strikes (.*)=/Uis", $tracnopara, $temp);
		$ttotalstrikes = $temp[1];

        //$tactivity[x] Last recorded activity

		preg_match_all("/recorded activity (.*)=/Uis", $tracnopara, $temp);
		$tactivity = $temp[1];

// Set working for the header based on number of thunderstorms

		if ($tnumber[1] == 1){
		} else {

			// Sometimes the TRACreport is empty so we need to say that

			//$tracreport = "<br />No data currently available or thunderstorms detected.<br/>";
		}

		$stormIntensity = '';

           switch($tintensity)
{
              case 'weak':
                    $stormIntensity = 'rgb(255, 255, 0)';
                             break;
              case 'moderate':
                    $stormIntensity = 'rgb(255, 102, 0)';
                             break;
              case 'strong':
                    $stormIntensity = 'rgb(255, 0, 0)';
                             break;
              case 'severe':
                    $stormIntensity = 'rgb(217, 0, 126)';

}

// This builds each line of text for each thunderstorm
// begin your table
$tracreport .=  "<div id='tracReport'><table style='{$tracTable}'>
        <tr>
            <td style='{$td1Style}'> Tracking " . $tnumber[1] . " active thunderstorms</td>
        </tr>
        <tr>
            <td>
                <table width='100%' cellpadding='0' cellspacing='0'>
                        <tr>
                            <td style='{$tr1Style}'>Cell ID</td>
                            <td style='{$tr1Style}'>Tracking Since</td>
                            <td style='{$tr1Style}'>Bearing</td>
                            <td style='{$tr1Style}'>Distance</td>
                            <td style='{$tr1Style}'>Intensity</td>
                            <td style='{$tr1Style}'>Trend</td>
                            <td style='{$tr1Style}'>Current Rate</td>
                            <td style='{$tr1Style}'>Peak Rate</td>
                            <td style='{$tr1Style}'>Total</td>
                            <td style='{$tr1Style}'>Last Strike</td>
                        </tr>"; // break the top part of the table
// create your loop
foreach($tid as $k => $v){
         $tracreport .= "<tr>
                            <td style='{$tr2Style}'>" . $tid[$k] . "</td>
                            <td style='{$tr2Style}'>" . $tdtime[$k] . "</td>
                            <td style='{$tr2Style}'>" . $tdirection[$k] . "  °</td>
                            <td style='{$tr2Style}'>" . $tdistance[$k] . " miles</td>
                            <td style='{$intStyle}'>" . strtolower($tintensity[$k]) . "</td>
                            <td style='{$tr2Style}'>" . trim(strtolower($ttrend[$k])) . "</td>
                            <td style='{$tr2Style}'>" . $tcurraterate[$k] . "</td>
                            <td style='{$tr2Style}'>" . $tpeakrate[$k] . "</td>
                            <td style='{$tr2Style}'>" . $ttotalstrikes[$k] . "</td>
                            <td style='{$tr2Style}'>" . trim(strtolower($tactivity[$k])) . "</td>
                        </tr>";
}  // close your loop
   // finish your table
        $tracreport .= "</table>
                    </td>
                </tr>
            </table></div>";

      }

echo $tracreport;

?>

Which in turn produces this.....

 

http://www.mesquiteweather.net/tracreportNS.php

 

Now what I am wanting to do is take the text that is under the "Intensity" and "Trend" column and assign colors to them depending on what the text is. I.E. Weak = green, Moderate = yellow etc, etc. 

 

BUT, when I insert the variable from the switch into the styling for that row it doesn't change the text color. 

 

So, what am I missing or doing wrong? I have done this many times in the past and it has worked just fine. Just not sure what I am overlooking and why it is not working this time

 

Does that explain things better? 

Link to comment
Share on other sites

When you finish this project, I highly recommend you read up on how to truly use CSS.  Embedding your entire redundant style= string into a php var which is going to also be redundant(!) is not the way to do it.  IMHO.

 

Guessing here - but I don't think you need the quotes on the color variable.  Just look at the previous color setting you have made:  color:$FFF.   No quotes there.

 

What puzzles me about the strtolower (and it is not what the function call does) thing is you are converting a string that your code seems to be responsible for producing, so why not just create it in lower case - WHICH you are smart enough to already be doing, so why the function call?

 

Lastly - forgive me if I'm still not understanding - you seem to be setting the var stormintensity into the style string you build,but it has no defined value at that point.  Therefore, you are not assigning any (override) color based on content.

Link to comment
Share on other sites

When you finish this project, I highly recommend you read up on how to truly use CSS.  Embedding your entire redundant style= string into a php var which is going to also be redundant(!) is not the way to do it.  IMHO.

 

 

 

I don't need CSS lessons. I am fully aware how CSS works. I have a degree in Web Development and have been doing it for 10+ years. Besides what your suggesting I am not doing. Did you not look at the full cleaned up code I posted? I don't think you're reading or looking at anything I am posting before answering. There is a reason I was doing it the way I was doing it and I didn't come here to get critiqued for that as it is irrelevant to my question. I am fully aware of how to use CSS, I am just not that fluent in PHP and what I do know about PHP I have self taught myself so give me a little credit. 

 

Guessing here - but I don't think you need the quotes on the color variable.  Just look at the previous color setting you have made:  color:$FFF.   No quotes there.

 

 

What color variable are you referring to? I don't have a color variable "$FFF", do you mean #FFF? If so of course #FFF isn't going to have quotes around it. 

 

What puzzles me about the strtolower (and it is not what the function call does) thing is you are converting a string that your code seems to be responsible for producing, so why not just create it in lower case - WHICH you are smart enough to already be doing, so why the function call?

 

 

I don't think you understand what "strtolower" does so let me explain it to you which I shouldn't even be doing anyways as it has NOTHING to do with my question other than you wanting to queston why I am doing something when you're not familiar with what it does which you shouldn't even be doing since it irrelevant to my question. Why am I not just creating it in lower case you ask? Because the software that produces and uploads the text file to my server in which I am exploding the data from produces it in CAPS. I have no control over that, that is just how the text file is created from the software and it is uploaded. So if I was just to use that variable that I created to pull that data from the text file it would output it in CAPS. I don't want it in caps. Seeing that there is 4 possible data outputs for that variable it is easier to just use "strtolower" with that variable so that the are in all lower case.  So you take the variable that you have created from the preg_match for that data and you use it like strtolower($ttrend) for example, it will now take that data within that variable and make it all lower case when it is outputted. Why? because that is what I want. I want it in lower case and that is the only way to do that since the data source produces it in CAPS which I don't want. So I can't create it in lower case because I have no control on how the data is produced in the text file. Again, irrelevant to my problem. 

 

Lastly - forgive me if I'm still not understanding - you seem to be setting the var stormintensity into the style string you build,but it has no defined value at that point.  Therefore, you are not assigning any (override) color based on content.

 

 

 

Again, unless you're not looking at the code I am posting or I am not understanding what you are saying but, the var does have a defined value as it is grabbing it from switch which is where var stormintensity is created from to start with. 

 

I thankk you for attempting to answer my question but all you're doing is complicating my question and I don't believe you are skilled enough to resolve this issue for me. So please refrain from responding further so those that so have a solution to my problem can offer it without you complicating this post any further with irrelevant questions. 

 

-Thanks

Link to comment
Share on other sites

Hope this helps

<?php




           switch($_POST[tintensity])
{
              case 'weak':
                    $stormIntensity = "#9C3";
                             break;
              case 'moderate':
                    $stormIntensity = "#09F";
                             break;
              case 'strong':
                    $stormIntensity = "#F60";
                             break;
              case 'severe':
                    $stormIntensity = "#F00"; break;
              
              default : $stormIntensity = ''; break;
}

?>
<table>
<tr>

<td style='background-color: <?=$stormIntensity?>; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;'>Storm intensity color  Background</td>
</tr>
</table>
<br />
enter weak, moderate, strong, severe<br />
<br />
<form name="colorshow" method="post">
<input type="text" name="tintensity"/>
<input type="submit" name="go"  value="Submit"/>
</form>

Link to comment
Share on other sites

  • Solution

@Texan78

 

Looking at your code in post #10. You need to use the switch statement for choosing the color of the storm intensity within the foreach loop on line 148, You'd pass strtolower($tintensity[$k]) as the condition and do a string replacement on the value of $intStyle replacing color: #FFF with color: $stormIntensity to colorize the storm intensity text

 

Code for the foreach loop (line 133)

// create your loop
foreach($tid as $k => $v){
        // pass in storm intensity for current storm
        switch(strtolower($tintensity[$k]))
        {
            case 'weak':
                $stormIntensity = 'rgb(255, 255, 0)';
                break;
            case 'moderate':
                $stormIntensity = 'rgb(255, 102, 0)';
                break;
          case 'strong':
                $stormIntensity = 'rgb(255, 0, 0)';
                break;
          case 'severe':
                $stormIntensity = 'rgb(217, 0, 126)';
                break;
        }
        // replace default color with $stormIntensity
        $intStyleCSS = str_replace('color: #FFF;', "color: $stormIntensity;", $intStyle);

        $tracreport .= "<tr>
                            <td style='{$tr2Style}'>" . $tid[$k] . "</td>
                            <td style='{$tr2Style}'>" . $tdtime[$k] . "</td>
                            <td style='{$tr2Style}'>" . $tdirection[$k] . "  °</td>
                            <td style='{$tr2Style}'>" . $tdistance[$k] . " miles</td>
                            <td style='{$intStyleCSS}'>" . strtolower($tintensity[$k]) /* $intSyleCSS applies the css for each storm */ . "</td>
                            <td style='{$tr2Style}'>" . trim(strtolower($ttrend[$k])) . "</td>
                            <td style='{$tr2Style}'>" . $tcurraterate[$k] . "</td>
                            <td style='{$tr2Style}'>" . $tpeakrate[$k] . "</td>
                            <td style='{$tr2Style}'>" . $ttotalstrikes[$k] . "</td>
                            <td style='{$tr2Style}'>" . trim(strtolower($tactivity[$k])) . "</td>
                        </tr>";
}  // close your loop

Also remove  color: ' . $stormIntensity . ' from line18 so it is

    $intStyle = "background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;";
Edited by Ch0cu3r
Link to comment
Share on other sites

 

@Texan78

 

Looking at your code in post #10. You need to use the switch statement for choosing the color of the storm intensity within the foreach loop on line 148, You'd pass strtolower($tintensity[$k]) as the condition and do a string replacement on the value of $intStyle replacing color: #FFF with color: $stormIntensity to colorize the storm intensity text

 

Code for the foreach loop (line 133)

// create your loop
foreach($tid as $k => $v){
        // pass in storm intensity for current storm
        switch(strtolower($tintensity[$k]))
        {
            case 'weak':
                $stormIntensity = 'rgb(255, 255, 0)';
                break;
            case 'moderate':
                $stormIntensity = 'rgb(255, 102, 0)';
                break;
          case 'strong':
                $stormIntensity = 'rgb(255, 0, 0)';
                break;
          case 'severe':
                $stormIntensity = 'rgb(217, 0, 126)';
                break;
        }
        // replace default color with $stormIntensity
        $intStyleCSS = str_replace('color: #FFF;', "color: $stormIntensity;", $intStyle);

        $tracreport .= "<tr>
                            <td style='{$tr2Style}'>" . $tid[$k] . "</td>
                            <td style='{$tr2Style}'>" . $tdtime[$k] . "</td>
                            <td style='{$tr2Style}'>" . $tdirection[$k] . "  °</td>
                            <td style='{$tr2Style}'>" . $tdistance[$k] . " miles</td>
                            <td style='{$intStyleCSS}'>" . strtolower($tintensity[$k]) /* $intSyleCSS applies the css for each storm */ . "</td>
                            <td style='{$tr2Style}'>" . trim(strtolower($ttrend[$k])) . "</td>
                            <td style='{$tr2Style}'>" . $tcurraterate[$k] . "</td>
                            <td style='{$tr2Style}'>" . $tpeakrate[$k] . "</td>
                            <td style='{$tr2Style}'>" . $ttotalstrikes[$k] . "</td>
                            <td style='{$tr2Style}'>" . trim(strtolower($tactivity[$k])) . "</td>
                        </tr>";
}  // close your loop

Also remove  color: ' . $stormIntensity . ' from line18 so it is

    $intStyle = "background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;";

 

 

Thanks for the suggestion it is greatly appreciated  It makes complete sense and I didn't even think about adding it in the loop. Unfortunately it still isn't working and it all looks right and the logic makes sense. It wouldn't matter that I am imploding a text file or how I am doing it would it? Something is definitely strange because there are other things I have done in the past that aren't working ether but that is a separate issue. With the suggestion above it causes the text to not appear at all. 

 

http://www.mesquiteweather.net/tracreportNS.php

<?php

// The TRACreportshort takes Nexstorm's full TRACreport and shortens it to a more
// managable size for displaying.
//
// It chops up the vital storm cell info, places it in arrays, and redisplays it as we wish
//

// Set some default values

$measure = "mi"; // km, nm, or mi depending on your Nexstorm measurements

// Let's assign the tables some styles
    $tracTable = "width:100%; margin-top: 15px; background-color:#000; color:#FFF;";
    $td1Style = "padding: 3px 0px 3px 0px; font-size: .9em; text-align: center; color: #00FF00;";
    $tr1Style = "background-color:#333; color:#3299cc; font-weight: normal; text-align: center; font-size: .9em;";
    $tr2Style = "background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;";
    $intStyle = "background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em;";

// Load the TRACreport and remove carriage returns, change the word 'no change' to 'steady'
$trac = implode('', file('lightning/nexstorm/TRACReport.txt'));
$tracnopara = preg_replace( '|\n|', '=', $trac );
$tracnopara = preg_replace( '|No change|', 'Steady', $tracnopara );
$tracnopara = preg_replace( '|/minute|', '  a min', $tracnopara);

// Intialize values
$tracreport = "";
$tnumber = 0;
$alertflag = 0;
$tracwarning = "";

// If there aren't any thunderstorms, then say it

if (preg_match("/No thunderstorms detected/i", $trac)) {
		$tracreport = "<table style='{$tracTable}'>
			<tr>
			    <td style='{$td1Style}'>No nearby thunderstorms detected</td>
			</tr>
               </table>"; }
	else {

// If there are thunderstorms, process them, putting them into various arrays

		// $tnumber[x] is for number of thunderstorms

		preg_match("/Tracking (.*) thunderstorms/Uis", $tracnopara, $tnumber);

		// $tid[x] is the ID number of the storm

		preg_match_all("/Thunderstorm ID (.*) detected/Uis", $tracnopara, $temp);
		$tid = $temp[1];

		// $tdtime[x] is the time detected

		preg_match_all("/detected (.*)=/Uis", $tracnopara, $temp);
		$tdtime = $temp[1];

		// $tdirection[x] is the direction of the storm

		preg_match_all("/Storm location bearing (.*) dgr/Uis", $tracnopara, $temp);
		$tdirection = $temp[1];

		// $tdistance[x] is the distance in $measure amounts

		preg_match_all("/distance (.*) $measure/Uis", $tracnopara, $temp);
		$tdistance = $temp[1];

		//$tintensity[x] is the intensity of the storm

		preg_match_all("/Intensity class (.*)=/Uis", $tracnopara, $temp);
		$tintensity = $temp[1];

		//$ttrend[x] is the trend of the storm

		preg_match_all("/Intensity trend (.*)=/Uis", $tracnopara, $temp);
		$ttrend = $temp[1];

		//$tcurraterate[x] Current strikerate

		preg_match_all("/current strikerate (.*)=/Uis", $tracnopara, $temp);
		$tcurraterate = $temp[1];

		//$tpeakrate[x] Peak strikerate

		preg_match_all("/peak strikerate (.*)=/Uis", $tracnopara, $temp);
		$tpeakrate = $temp[1];

		//$tpeakrate[x] Total recorded strikes

		preg_match_all("/total recorded strikes (.*)=/Uis", $tracnopara, $temp);
		$ttotalstrikes = $temp[1];

        //$tactivity[x] Last recorded activity

		preg_match_all("/recorded activity (.*)=/Uis", $tracnopara, $temp);
		$tactivity = $temp[1];

// Set working for the header based on number of thunderstorms

		if ($tnumber[1] == 1){
		} else {

			// Sometimes the TRACreport is empty so we need to say that

			//$tracreport = "<br />No data currently available or thunderstorms detected.<br/>";
		}

// This builds each line of text for each thunderstorm
// begin your table
$tracreport .=  "<div id='tracReport'><table style='{$tracTable}'>
        <tr>
            <td style='{$td1Style}'> Tracking " . $tnumber[1] . " active thunderstorms</td>
        </tr>
        <tr>
            <td>
                <table width='100%' cellpadding='0' cellspacing='0'>
                        <tr style='{$tr1Style}'>
                            <td>Cell ID</td>
                            <td>Tracking Since</td>
                            <td>Bearing</td>
                            <td>Distance</td>
                            <td>Intensity</td>
                            <td>Trend</td>
                            <td>Current Rate</td>
                            <td>Peak Rate</td>
                            <td>Total</td>
                            <td>Last Strike</td>
                        </tr>"; // break the top part of the table


// create your loop
foreach($tid as $k => $v){

        // pass in storm intensity for current storm
        switch($tintensity[$k])
        {
            case 'weak':
                $stormIntensity = 'rgb(255, 255, 0)';
                break;
            case 'moderate':
                $stormIntensity = 'rgb(255, 102, 0)';
                break;
          case 'strong':
                $stormIntensity = 'rgb(255, 0, 0)';
                break;
          case 'severe':
                $stormIntensity = 'rgb(217, 0, 126)';
                break;
        }

        // replace default color with $stormIntensity
        $intStyleCat = str_replace('color: #FFF;', "color: $stormIntensity;", $intStyle);

        $tracreport .= "<tr>
                            <td style='{$tr2Style}'>" . $tid[$k] . "</td>
                            <td style='{$tr2Style}'>" . $tdtime[$k] . "</td>
                            <td style='{$tr2Style}'>" . $tdirection[$k] . "  °</td>
                            <td style='{$tr2Style}'>" . $tdistance[$k] . " miles</td>
                            <td style='{$intStyleCat}'>" . $tintensity[$k] . "</td>
                            <td style='{$tr2Style}'>" . $ttrend[$k] . "</td>
                            <td style='{$tr2Style}'>" . $tcurraterate[$k] . "</td>
                            <td style='{$tr2Style}'>" . $tpeakrate[$k] . "</td>
                            <td style='{$tr2Style}'>" . $ttotalstrikes[$k] . "</td>
                            <td style='{$tr2Style}'>" . $tactivity[$k] . "</td>
                        </tr>";
}  // close your loop

   // finish your table
        $tracreport .= "</table>
                    </td>
                </tr>
            </table></div>";

      }

echo $tracreport;

?>
Edited by Texan78
Link to comment
Share on other sites

You haven't applied strtolower to $tintensity[$k] for the switch condition

// pass in storm intensity for current storm
switch($tintensity[$k])

// it should read
switch(strtolower($tintensity[$k]))

EDIT Just looked at your data file, try applying trim too. White space maybe included

switch(strtolower(trim($tintensity[$k])))
Edited by Ch0cu3r
Link to comment
Share on other sites

That is exactly what I am needing. 

 

And, this didn't peak your interest at all?--  because it's pretty badass and really what you should have wanted to learn!

It makes useful use of your users' computer power and saves your server from doing extra stuff it really doesn't need. It also reduces the overall size of the transfer! Stop polluting the internet, thanks! ;]

<style>
.storm { background-color: black; color: #FFF; border: 1px #666 solid; text-align: center; font-size: .9em; }
.storm.weak { color: rgb(255, 255, 0); }
.storm.strong { color: rgb(255, 0, 0); }
</style>

<?php
echo '<td class="storm ' . $tintensity . '">' . $tintensity . '<td>';
Edited by objnoob
Link to comment
Share on other sites

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.