Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question use php to turn html table data to php variables? RESOLVED

    hi, i am trying to extract a column of weather data and turn it into a line graph.

    i have the graph part already sorted - it just needs raw data inputted as php variables.

    the data comes from a html table at

    http://wxweb.meteostar.com/sample/sa...tml?text=EGRR/

    in particular i am trying extract the data from the column called "valid time" and also the corresponding data from the "500-1000 THKNS" column.

    the resultant variables would just be called $x and $y, for example the variables with data would end up looking like..

    $x1 = Fri 01/06 15Z
    $x2 = Fri 01/06 18Z
    $x3 = Fri 01/06 21Z

    etc etc

    and also

    $y1 = 544
    $y2 = 544
    $y3 = 545


    how easy is this to do using php and could someone please help me?
    Last edited by muppet77; 01-07-2012 at 01:24 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,540
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Before you go through all of this, did you ask them if they provide
    the data in the form of a CSV (comma separated variable) or XML format?
    If they did, that would be the best solution.

    But if that HTML <table> is all they have for a data report, you're stuck
    with parsing it all out. It can be done, but ask about the format first.
    If they only have the HTML table, they should have someone program a
    different format option ... which would be fairly easy on their side of it.

    Let us know.

  • Users who have thanked mlseim for this post:

    muppet77 (01-06-2012)

  • #3
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts
    thanks mate.

    it is just a web site that I found and there is no evidence of csv else it would be linked.

    how would I go about it with just the format above?

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,540
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    There are two separate sections of 500-1000 THKNS ...
    Do you need both sections?

    And would it be OK to only pick the one's that are links (every other one?).
    Picking the one with links is easy because those lines have a unique string in them.

  • #5
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts
    yes, unfortunately i do need both sections of THCKNS (as with the dates too)

    ideally i'd like every value but if the linked ones are easier then so be it.


  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,540
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    So, below is my script.
    It's not perfect. The only unique thing on the lines we want to parse are the "SLP_THK_RH" strings.
    That means I have to grab the line before it too, just assuming it's a value without a link.
    At the end, we don't know if the list ends with a "SLP_THK_RH" string or not, so the last value may be blank.

    Anyhow, try this out.

    PHP Code:
    <?php

    // site url 
    $site_url "http://wxweb.meteostar.com/sample/sample_C.shtml?text=EGRR/#"

    // With my webhost, I am not allowed to use fopen() or get_contents.
    // So, I use the preferred CURL method.  This depends on your webhost and PHP revision.

    // curl get contents 
    $ch curl_init(); 
    $timeout 5// set to zero for no timeout 
    curl_setopt ($chCURLOPT_URL$site_url); 
    curl_setopt ($chCURLOPT_RETURNTRANSFER1); 
    curl_setopt ($chCURLOPT_CONNECTTIMEOUT$timeout); 
    $file_contents curl_exec($ch); 
    curl_close($ch); 
    $lines = array(); 
    $lines explode("\n"$file_contents); 

    // define a couple of arrays,
    // one for the time, and the other for the actual value.
    $time=array();
    $value=array();

    $q=0;
    $i=0;
    // loop thru and find the specific text within each line.
    // the only thing to key from is "SLP_THK_RH", and they only
    // appear on the lines with a link for the value.
    foreach($lines as $line) {
       if(
    strpos($line,"SLP_THK_RH")){ 
       
    $string=strip_tags($line);   
       
    $value[$q]=$temp;
       
    $value[$q+1]=$string;
       
    $q=$q+2;
       }
       
    $temp=$line;
       
    $temp=strip_tags($temp);
       if(
    strpos($line,"Z<")){
       
    $string2=strip_tags($line);   
       
    $time[$i]=$string2;
       
    $i++;
       }


    // You'll miss the last value if it's not a link,
    // because that line won't have "SLP_THK_RH" in the string.
    // So, your data will be short by one value.
    // We can't be sure if the last line will ever be a link or not.

    // Display the actual arrays ...
    print_r($time);
    echo 
    "<br><br>";
    print_r($value);
    echo 
    "<br><br>";

    // Display results ...
    // Notice that time starts with one extra line because "Z<" appears on a table title line.
    // So, the $time array is offset by 1.  
    for($x=0$x<=count($value); $x++){
    echo 
    "TIME: {$time[$x+1]} ... VALUE: {$value[$x]} <br />";
    }

    ?>
    NOTE:

    You could also use Google Chart to create your line graph.
    The PHP would be generating the javascripting "on-the-fly".
    I do that same thing with Yahoo Weather. I process their XML
    file and generate the javascripting for Google Chart:
    http://www.catpin.com/weather/temp_humid.php

    You can view the HTML source to see how the values are put into the chart.


    .
    Last edited by mlseim; 01-06-2012 at 11:21 PM.

  • #7
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts
    thank you so much!

    however, when i add


    print $x1 $y1;
    at the end of the code, it doesn't print them?

    (my second code relies on the variables being created in this way)

    eg if i call your code snow.php, my second code may be

    <?php


    include ("snow.php");

    print $x1;
    print $y1;


    ?>
    which expectedly returns your output again (as i didn't hash out the print bits of your code) but not the $x1 and $y1 as i added in?

    can you please help?

    thanks

  • #8
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts
    EDIT =

    just realised they are stored in arrays, so i can call them with

    echo $time[6];
    echo $value[6];


    thanks!!!!

  • #9
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts
    out of interest is there a way I can turn one of the array into a variable?

    this seemed obvious but doesn't work.

    $v1 == $value[4];

    any ideas. I had a search but couldn't get a clear answer at my level.

  • #10
    New Coder
    Join Date
    Dec 2011
    Location
    Reading, Berkshire, UK
    Posts
    36
    Thanks
    7
    Thanked 0 Times in 0 Posts
    for referece for others i successfully used:

    extract($time, EXTR_PREFIX_ALL, 'time');
    extract($value, EXTR_PREFIX_ALL, 'value');


    print " $value_0 $value_1 $value_2 $value_3 $value_4 ";
    print " $time_0 $time_1 $time_2 $time_3 $time_4 ";

  • #11
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,540
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    You don't want to ... or shouldn't turn the array into variables ... why would you do that?

    You can print the array values just like you do the "variables" you are trying to use.

    I don't get it.

  • #12
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,540
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Which temperature columns(s) are you trying to get?
    Let me know what the column heading is on the ones you want.


  •  

    Posting Permissions

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