Have a weird question regarding functions in PHP (maybe?) - I have a "web-app" that pulls climate data and applies "conditional formatting" to different temperature variables. $High, $Low, $xTmp, and $xDew. I currently have 4 cumbersome case loops and would really like to consolidate the code to just a single case loop, and this is where I am wondering if a function would be useful. I am having some challanges though, as I can't seem to 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) into this loop, and color-coat 2 different rows 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>";
}
Instead I am getting back blank results.
The code worked fine not in a function, but again, that's an extra 100 lines of code I would like to remove.
I changed a few things and the following 3v4l is working 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 a single integer parameter, from your code it looks as though you are passing in a color?
This would be my approach (I have tested the below code and it works fine).
Have an array to control all your temperatures and colours.
The array only needs to store the lowest value of each temperature range which share the same colour.
For example, you have the 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 that range and all share the same colour.
So there's no need to have -11, -12, -13, or -14 in the array because my suggested script simply works backwards from your current temperature until it reaches a match in the array for the lowest number in that range.
Hopefully the code will make it more understandable:
// 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++; }
}
The while will stop at your lowest temperature (for sanity and so no infinite loops).
If the current temperature is not found in the array, then drop the temporary temperature variable down 1 each loop until it reaches a value which is in the array.
Deducting 1 each time will simply lower the temperature until it hits the lowest temperature for the actual temperatures group/range, and so provides you with the shared colour.
There are all sorts of approaches, but this way you can add or remove temperatures from the array, and control the colours for each temperature range/group too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With