Jump to content

Help: simple_html_dom.php select first table row only


aussie

Recommended Posts

Gidday all,

 

My Utimate goal is to parse the data on the first row in first table and first row in second table.

from here: http://www.bom.gov.au/products/IDQ60901/IDQ60901.94580.shtml

 

Presently I can only parse data in the last row in the last table.

I got to this point about 2 days ago, I am unable to find any info as to what I need to do to achieve what I want.

some of the info I've found I don't understand.

 

Need newbie help.

 

What do I need to add/change to parse the data in  at least the first table row?

 


<?php
error_reporting(E_ALL);
include_once('htmldom/simple_html_dom.php');
$url = 'http://www.bom.gov.au/products/IDQ60901/IDQ60901.94580.shtml';


// Create DOM from URL
$html = file_get_html($url);

foreach($html->find('table tr') as $weather) {
    if($weather->find('th')) {continue;} //apparently this needs to be added because there is a bug in simple_html_dom.php
    if(!$weather->find('td ', 0)) {continue;}
    
    $datetime = $weather->find('td', 0)->plaintext;
    $currentTemp = $weather->find('td', 1)->plaintext;

}

print_r('updated:' . '&nbsp' .$datetime);
print_r ('<br>');
print_r('CurrentTmp:' . '&nbsp' .$currentTemp);
print_r ('<br>');
?>

Link to comment
Share on other sites

foreach($html->find('table tr') as $weather) {
    if($weather->find('th')) {continue;} //apparently this needs to be added because there is a bug in simple_html_dom.php
    if(!$weather->find('td ', 0)) {continue;}
    
    $datetime = $weather->find('td', 0)->plaintext;
    $currentTemp = $weather->find('td', 1)->plaintext;

}

 

What you are doing here is over writing the value of $datetime and $currentTemp for every instance of <tr> on the source page.  Only after it has finished looking up all the <tr>'s are you then using the final <tr>'s information.

 

how does it work if you do this:

<?php
error_reporting(E_ALL);
include_once('htmldom/simple_html_dom.php');
$url = 'http://www.bom.gov.au/products/IDQ60901/IDQ60901.94580.shtml';


// Create DOM from URL
$html = file_get_html($url);
for ($i=0; $i <2; $i++){
foreach($html->find('table tr') as $weather) {
    if($weather->find('th')) {continue;} //apparently this needs to be added because there is a bug in simple_html_dom.php
    if(!$weather->find('td ', 0)) {continue;}
    
    $datetime = $weather->find('td', 0)->plaintext;
    $currentTemp = $weather->find('td', 1)->plaintext;

print_r('updated:' . '&nbsp' .$datetime);
print_r ('<br>');
print_r('CurrentTmp:' . '&nbsp' .$currentTemp);
print_r ('<br>');
}
}
?>

Link to comment
Share on other sites

Thanks for your answer

for ($i=0; $i <2; $i++){

I figured out I had to do something like this. This almost gave me what I was after.

 

But too many results,

 

I figured out if I put

 break;

I was able to just get the result I wanted.

 

Thanks so much

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.