View Full Version : Simple PHP file needs help!
Gulfbreeze
12-08-2010, 03:25 AM
I'm trying to write a small but effective php script.
What I want to do is be able to take METAR abbreviations, and have then translate to their full title name. (IE --> (SKC) to read as (Clear Skies))
<?PHP
$sky=%[wuhu]METAR_sky%; // rename [wuhu]METAR_sky variable which gets Metar sky conditions, and put into $sky variable.
IF $sky=="SKC" then print "Clear Skies"; // IF $sky variable reports back with SKC, which stand for Clear skies. Then print Clear Skies.
?>
WUHU is a weather software program - the Metar (see below) is picked up, and is put into the variable form of %[wuhu]METAR_sky%.
METAR Sky Codes are one of the following:
METAR........SKY Condition
=====================
SKC ...........Clear Skies
FEW............Few Clouds
SCT............Partly Cloudy
BKN............Mostly Cloudy
OVC...........Cloudy
I think I'm on the right track, but I'm no good at php, and would like some assistance please!
Thanks -- GB
djr33
12-08-2010, 05:04 AM
Once you have $sky defined, you can 'translate' it using a function. Try something like this:
<?php
function skyname($sky) {
if ($sky=='SKC') { return 'Clear Sky'; } //found a match, return this value and stop looking
if ($sky=='FEW') { return 'Few Clouds'; }
//...
return 'Default Sky Condition'; //default here, whatever you want in case there is an error
}
$sky = %[wuhu]METAR_sky%;
echo skyname($sky);
?>
That's not necessarily the most efficient way to do it, but it's probably the easiest since you are learning the basics now.
By the way, I've never seen the syntax of %[wuhu]META_sky%, so I'm just going to assume that your script is special for some reason and that works. If you have problems with this, just try using echo $sky; below that to be sure you are first getting the correct 3-letter code, then you can deal with translating it if it's correct.
the abbreviations / definitions should be stored in an array
$metar = array(
'SKC'=>'Clear Skies'
,'FEW'=>'Few Clouds'
,'SCT'=>'Partly Cloudy'
,'BKN'=>'Mostly Clody'
,'OVC'=>'Cloudy'
);
and you could check for a match and print the definition like so:
if(array_key_exists($sky, $metar)){
print $metar[$sky];
}
however, I'm not clear on how you're getting to this point. How is the "wuhu" variable getting into the php script? are you using php from the command line (is %[wuhu]% a DOS variable (or similar))?
it seems that it would cause problems in PHP (where % is the modulus operator), but if it works, then it works, I guess. I'd be interested in knowing more about what you're doing.
whup, Daniel beat me to it while I was trying to research the %variable% thing a little : )
Gulfbreeze
12-08-2010, 05:51 AM
Works perfectly Daniel...got one question other small question.
I just noticed this, the Metar variables, after the first one(SKC) include '000'
The 000 is for cloud height, which I don't need.
How do I get the coding to not see the ending 000?
<?php
function skyname($sky) {
if ($sky=='SKC') { return 'Clear Sky'; } //found a match, return this value and stop looking
if ($sky=='FEW000') { return 'Few Clouds'; }
if ($sky=='SCT000') { return 'Partly Cloudy';}
if ($sky=='BKN000') { return 'Mostly Cloudy';}
if ($sky=='OVC000') { return 'Cloudy';}
if ($sky=='RA') { return 'Rain';}
//...
return 'Sky Condition Unavailable'; //default here, whatever you want in case there is an error
}
$sky = %[wuhu]METAR_sky%;
echo skyname($sky);
?>
GB
Gulfbreeze
12-08-2010, 05:58 AM
the abbreviations / definitions should be stored in an array
$metar = array(
'SKC'=>'Clear Skies'
,'FEW'=>'Few Clouds'
,'SCT'=>'Partly Cloudy'
,'BKN'=>'Mostly Clody'
,'OVC'=>'Cloudy'
);
and you could check for a match and print the definition like so:
if(array_key_exists($sky, $metar)){
print $metar[$sky];
}
however, I'm not clear on how you're getting to this point. How is the "wuhu" variable getting into the php script? are you using php from the command line (is %[wuhu]% a DOS variable (or similar))?
it seems that it would cause problems in PHP (where % is the modulus operator), but if it works, then it works, I guess. I'd be interested in knowing more about what you're doing.
whup, Daniel beat me to it while I was trying to research the %variable% thing a little : )
Not sure exactly, how the guy who wrote WUHU does it, but the %variable% work both in HTML and PHP. I have the script that Daniel gave me up and running if you'd like to take a look, if it will help!.. See HERE (http://home.mchsi.com/~dougrd/skytest.php)
djr33
12-08-2010, 05:42 PM
My guess is that it must be some other language first parsing the script to dynamically generate PHP code; then the PHP dynamically generates HTML code. A complex cycle, but as traq said, if it works it works.
Did you solve the problem? I clicked the link and it appears to work.
You can do several things, but since the code can be 2-3 characters and have (I'm guessing) unpredictable numbers following it, that makes things more complicated.
One way to approach this is to use the following instead of simple equals:
if (strpos($sky,'SKC')===0) { return 'Clear Sky'; } //if the beginning of the code is 'SKC'
//same format for the rest, just change 'SKC'
//leave the default as-is
By the way, traq's method is a little more structured, but I suggested this way because it's a little simpler if your goal isn't to learn more advanced PHP yet and just get it working-- look into arrays because they can be very useful. Since this project only has a few values, maintaining the code shouldn't be difficult. If there were thousands, for example, an array (or even a database) would be very helpful.
you could also reduce $sky to the first three letters (assuming you'll never need more than the first three):
$sky = substr($sky,0,2);
// if $sky was originally "OVC011930872436...whatever",
// this changes the value to just "OVC".
and then proceed as you are.
It gets more complex if the sky codes are not always three letters long, but it appears that they are. Let me know if you have any exceptions.
djr33
12-08-2010, 09:23 PM
The 'RA' code (unless that happens to be a typo) makes that too complex. Otherwise that's easier. Making sure it's in initial position should be reliable regardless of the input (as long as it's in a similar format) in case they change things later, for example...
The 'RA' code (unless that happens to be a typo) makes that too complex. Otherwise that's easier. Making sure it's in initial position should be reliable regardless of the input (as long as it's in a similar format) in case they change things later, for example...
ohh, I missed that one. in the original post they were all three-letter codes. sorry!
$sky = preg_replace("/[0-9]/","",$sky);
// this removes all numbers from $sky
Gulfbreeze
12-09-2010, 01:41 AM
The 'RA' code (unless that happens to be a typo) makes that too complex. Otherwise that's easier. Making sure it's in initial position should be reliable regardless of the input (as long as it's in a similar format) in case they change things later, for example...
Yeah, sorry it was a typo...should be (-RA) or (VV) my bad. :rolleyes: Yes, need only the first three characters and ignore the rest. No, I didn't get it fixed yet, took your coding and and added in the 000 part, so it would do right.
<?PHP
function skyname($sky) {
if ($sky=='SKC') { return 'Clear'; } //found a match, return this value and stop looking
if ($sky=='FEW000') { return 'Fair'; }
if ($sky=='SCT000') { return 'Partly Cloudy';}
if ($sky=='BKN000') { return 'Mostly Overcast';}
if ($sky=='OVC000') { return 'Overcast';}
if ($sky=='-RA') { return 'Rain';} //can also use VV000 for this code alternate Metar code for Rain
//...
return 'Sky Conditions Unavailable.'; //default here, whatever you want in case there is an error
}
$sky = %[wuhu]METAR_sky%;
echo skyname($sky);
?>
that would only work if the numbers were always "000" (which I don't think is the case). If your sky codes are always 3 letters long, the substr() function would be the easiest solutions.
Gulfbreeze
12-10-2010, 05:57 AM
Thank you both for responding so quickly...I got it all setup, and it works perfectly.
I don't know if you guys are familiar with the Sager Forecasting java script or not, but that's what I'm trying to rewrite, to accepting the data from my weather station, as verse having to manually enter it.
So beware..I may be asking how-to questions, as I go along. :)
GB
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.