PHP 5 Functions - Can I Do It?

Is there a weird question about PHP functions (maybe?) - I have a "web application" that pulls in climate data and applies "conditional formatting" to different temperature variables. $ High, $ Low, $ xTmp and $ xDew. I currently have 4 cumbersome cases and would really like to consolidate the code in just one loop and am wondering if the function would be useful. However, I have some problems as I cannot get the function to work.

Here is the code:

function ColorTemp($PassThisTemp) 
{

global $ThisColor; global $ThisTemp;
$ThisTemp = $PassThisTemp; $ThisColor = ''; switch (true) {
case ($ThisTemp <= -20): $ThisColor = 'C6TB20'; break;
case ($ThisTemp <= -15) and ($ThisTemp >= -19): $ThisColor = 'C6TB15B19'; break;
case ($ThisTemp <= -10) and ($ThisTemp >= -14): $ThisColo = 'C6TB10B14'; break;
case ($ThisTemp <= -5) and ($ThisTemp >= -9): $ThisColor = 'C6TB05B09'; break;
case ($ThisTemp <= 0) and ($ThisTemp >= -4): $ThisColor= 'C6T000B04'; break;
case ($ThisTemp <= 4) and ($ThisTemp >= 1): $ThisColor = 'C6T004001'; break;
case ($ThisTemp <= 9) and ($ThisTemp >= 5): $ThisColor = 'C6T009005'; break;
case ($ThisTemp <= 14) and ($ThisTemp >= 10): $ThisColor = 'C6T014010'; break;
case ($ThisTemp <= 19) and ($ThisTemp >= 15): $ThisColor = 'C6T019015'; break;
case ($ThisTemp <= 24) and ($ThisTemp >= 20): $ThisColor = 'C6T024020'; break;
case ($ThisTemp <= 29) and ($ThisTemp >= 25): $ThisColor = 'C6T029025'; break;
case ($ThisTemp <= 32) and ($ThisTemp >= 30): $ThisColor = 'C6T032030'; break;
case ($ThisTemp <= 34) and ($ThisTemp >= 33): $ThisColor = 'C6T034033'; break;
case ($ThisTemp <= 39) and ($ThisTemp >= 35): $ThisColor = 'C6T039035'; break;
case ($ThisTemp <= 44) and ($ThisTemp >= 40): $ThisColor = 'C6T044040'; break;
case ($ThisTemp <= 49) and ($ThisTemp >= 45): $ThisColor = 'C6T049045'; break;
case ($ThisTemp <= 54) and ($ThisTemp >= 50): $ThisColor = 'C6T054050'; break;
case ($ThisTemp <= 59) and ($ThisTemp >= 55): $ThisColor = 'C6T059055'; break;
case ($ThisTemp <= 64) and ($ThisTemp >= 60): $ThisColor = 'C6T064060'; break;
case ($ThisTemp <= 69) and ($ThisTemp >= 65): $ThisColor = 'C6T069065'; break;
case ($ThisTemp <= 74) and ($ThisTemp >= 70): $ThisColor = 'C6T074070'; break;
case ($ThisTemp <= 79) and ($ThisTemp >= 75): $ThisColor = 'C6T079075'; break;
case ($ThisTemp <= 84) and ($ThisTemp >= 80): $ThisColor = 'C6T084080'; break;
case ($ThisTemp <= 89) and ($ThisTemp >= 85): $ThisColor = 'C6T089085'; break;
case ($ThisTemp <= 94) and ($ThisTemp >= 90): $ThisColor = 'C6T094090'; break;
case ($ThisTemp <= 99) and ($ThisTemp >= 95): $ThisColor = 'C6T099095'; break;
case ($ThisTemp <= 104) and ($ThisTemp >= 100): $ThisColor = 'C6T104100'; break;
case ($ThisTemp <= 109) and ($ThisTemp >= 105): $ThisColor = 'C6T109105'; break;
case ($ThisTemp <= 114) and ($ThisTemp >= 110): $ThisColor = 'C6T114110'; break;
case ($ThisTemp >= 115): $ThisColor = 'C6T115'; break; }

}

      

I am trying to pass 2 variables (based on SQL results) to this loop and the colored ones are 2 different strings based on $ ThisColor

ColorTemp($row_OJCObsDate['dewpoint_f']);
ColorTemp($row_OJCObsDate['temp_f']);

echo "<tr>";
echo "<td>" . $row_OJCObsDate['ObsID'] . "</td>";
echo "<td>" . $row_OJCObsDate['station_id'] . "</td>";
echo "<td>" . $row_OJCObsDate['observation_time'] . "</td>";
echo "<td>" . $row_OJCObsDate['weather'] . "</td>";
echo "<td class='" . ColorTemp($ThisColor) . "'>" . ColorTemp($ThisTemp) . "</td>";
echo "<td class='" . ColorTemp($ThisColor) . "'>" . ColorTemp($ThisTemp) . "</td>";
echo "<td>" . $row_OJCObsDate['relative_humidity'] . "</td>";
echo "<td>" . $row_OJCObsDate['wind_dir'] . "</td>";
echo "<td>" . $row_OJCObsDate['wind_mph'] . "</td>";
echo "<td>" . $row_OJCObsDate['wind_gust_mph'] . "</td>";
echo "<td>" . $row_OJCObsDate['pressure_mb'] . "</td>";
echo "<td>" . $row_OJCObsDate['visibility_mi'] . "</td>";   
echo "</tr>";
}

      

I am returning empty results instead.

The code worked fine not in a function, but again, that's the extra 100 lines of code that I would like to remove.

+3


source to share


3 answers


I changed a few things and the following 3v4l works fine for me http://3v4l.org/B4E1l

<?php
/**
 * @var int $ThisTemp A temperature
 */
function ColorTemp($ThisTemp) 
{
    $ThisColor = ''; 
    switch (true) {
        case ($ThisTemp <= -20): $ThisColor = 'C6TB20'; break;
        case ($ThisTemp <= -15) and ($ThisTemp >= -19): $ThisColor = 'C6TB15B19'; break;
        ...
        case ($ThisTemp <= 114) and ($ThisTemp >= 110): $ThisColor = 'C6T114110'; break;
        case ($ThisTemp >= 115): $ThisColor = 'C6T115'; break; 
    }

    return $ThisColor;
}

echo ColorTemp(12);

      

I also noticed something strange with



echo "<td class='" . ColorTemp($ThisColor) . "'>" . ColorTemp($ThisTemp) . "</td>";
echo "<td class='" . ColorTemp($ThisColor) . "'>" . ColorTemp($ThisTemp) . "</td>";

      

ColorTemp () takes one integer parameter, from your code does it look like you are passing in color?

+1


source


This will be my approach (I checked the below code and it works great).

Have an array to control all your temperatures and colors.
The array should only store the lowest value for each temperature range that has the same color.

For example, you have ranges:

$ThisTemp <= -10 and $ThisTemp >= -14

So, your range is -10 to -14, with -10 being the lowest. And -10, -11, -12, -13, and -14 are all in this range, and all have the same color.

So there is no need to have -11, -12, -13, or -14 in the array, because my suggested script just runs from your current temperature until it reaches a match in the array for the lowest number in that region.



Hopefully the code makes it clearer:

// Temperature Array
$aryTempToColor = array(
"-20" => "C6TB20",
"-15" => "C6TB15B19",
"-10" => "C6TB10B14",
"-5" => "C6TB05B09",
"0" => "C6T000B04",
"4" => "C6T004001",
"9" => "C6T009005",
"14" => "C6T014010",
"19" => "C6T019015",
"24" => "C6T024020",
"29" => "C6T029025",
"32" => "C6T032030",
"34" => "C6T034033",
"39" => "C6T039035",
"44" => "C6T044040",
"49" => "C6T049045",
"54" => "C6T054050",
"59" => "C6T059055",
"64" => "C6T064060",
"69" => "C6T069065",
"74" => "C6T074070",
"79" => "C6T079075",
"84" => "C6T084080",
"89" => "C6T089085",
"94" => "C6T094090",
"99" => "C6T099095",
"104" => "C6T104100",
"109" => "C6T109105",
"114" => "C6T114110",
"115" => "C6T115",
);

$ThisTemp = 68; // Your current temperature, change the value for testing
$ArrayTemp = $ThisTemp;

while ( $ArrayTemp > -21 )
  {

    if ( array_key_exists($ArrayTemp, $aryTempToColor) )
      {
        $ThisColor = $aryTempToColor[$ArrayTemp];
        break;
      }

    if ( $ArrayTemp >= 0) { $ArrayTemp--;}
    else { $ArrayTemp++; }


  }

      

while

will stop at your lowest temperature (for saneness and therefore endless cycles).
If the current temperature is not found in the array, then remove the temporary temperature variable by 1 each cycle until it reaches the value that is in the array.

Subtracting 1 will simply lower the temperature each time until it reaches the lowest temperature for the actual temperature / range group, and therefore will provide you with an overall color.

There are all sorts of approaches, but you can add or remove temperatures from the array and control colors for each temperature range / group.

+1


source


I wonder what it is.

The only thing I can think of is to use elseif and start at the max and work backwards.

function getColour($ThisTemp)
    if($ThisTemp > 115)
    { 
        return 'C6T115';
    } elseif($ThisTemp > 110) {
        return 'C6T114110';
    } elseif($ThisTemp > 105) {
        // .... Etc etc
    } // .....
}
$ThisColour = getColour(112);

      

So you only need to put the top of the range.

0


source







All Articles