Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Simple PHP file needs help!

  1. #1
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    2
    Thanked 1 Time in 1 Post

    Question Simple PHP file needs help!

    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 Code:
    <?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

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Once you have $sky defined, you can 'translate' it using a function. Try something like this:

    PHP Code:
    <?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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    the abbreviations / definitions should be stored in an array
    PHP Code:
    $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:
    PHP Code:
    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.

    Edit:

    whup, Daniel beat me to it while I was trying to research the %variable% thing a little : )


  4. #4
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    2
    Thanked 1 Time in 1 Post

    Red face

    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 Code:
    <?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

  5. #5
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by traq View Post
    the abbreviations / definitions should be stored in an array
    PHP Code:
    $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:
    PHP Code:
    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.

    Edit:

    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

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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:
    PHP Code:
       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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    you could also reduce $sky to the first three letters (assuming you'll never need more than the first three):
    PHP Code:
    $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.
    Last edited by traq; 12-08-2010 at 06:12 PM. Reason: typo in substr() function (forgot the string!)

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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...
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by djr33 View Post
    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!
    PHP Code:
    $sky preg_replace("/[0-9]/","",$sky);
    // this removes all numbers from $sky 

  10. #10
    Join Date
    Jul 2010
    Posts
    14
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by djr33 View Post
    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. 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 Code:
    <?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);
    ?>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •