aussie Posted January 7, 2012 Share Posted January 7, 2012 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:' . ' ' .$datetime); print_r ('<br>'); print_r('CurrentTmp:' . ' ' .$currentTemp); print_r ('<br>'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/254526-help-simple_html_domphp-select-first-table-row-only/ Share on other sites More sharing options...
Muddy_Funster Posted January 9, 2012 Share Posted January 9, 2012 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:' . ' ' .$datetime); print_r ('<br>'); print_r('CurrentTmp:' . ' ' .$currentTemp); print_r ('<br>'); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254526-help-simple_html_domphp-select-first-table-row-only/#findComment-1305719 Share on other sites More sharing options...
aussie Posted January 13, 2012 Author Share Posted January 13, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254526-help-simple_html_domphp-select-first-table-row-only/#findComment-1307177 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.