Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Trying to get data from this poorly created HTML format

http://www.weather.gov.sg/lws/zoneInfo.do

All I need is to get data for 3 places, e.g. Bedok, City and Katong. How do I store the data in an array for this?

This is what I did to get the store the first 5 lines, which is not exactly what I want.

$row_counter='0';
while($row_counter<5)
{
$ret['Name'][] = $html->find('.FORM1', $row_counter)->innertext;
$ret['Area'][] = $html->find('.FORM1', $row_counter)->next_sibling()->innertext;
$ret['Alert'][] = $html->find('.FORM1', $row_counter)->next_sibling()->next_sibling()->innertext;
$ret['From'][] = $html->find('.FORM1', $row_counter)->next_sibling()->next_sibling()->next_sibling()->innertext;
$ret['Till'][] = $html->find('.FORM1', $row_counter)->next_sibling()->next_sibling()->next_sibling()->next_sibling()->innertext;
$row_counter++;
}

I am able to store data successfully for the whole row and all columns. What is the most efficient way to search for a certain name, e.g. Bedok and getting the columns beside it like next_sibling?

Thanks.

share|improve this question
    
what data you are expecting? –  user2193789 Apr 1 '13 at 10:36
    
I need the valid from and valid till data. It is now just a dash since there is no lightning risk, but it usually shows a time in it –  user2216416 Apr 1 '13 at 10:45
    
I didn't get you properly. maybe I am bad at English. are you trying to get all the data in an array? –  user2193789 Apr 1 '13 at 10:49
    
I want the data on the right of the specific area mentioned, e.g Bedok, Zone, -, - in an array. –  user2216416 Apr 1 '13 at 10:55
add comment

1 Answer

up vote 2 down vote accepted

Isn't it easy. Try things first then ask. (:

<?php
include 'simple_html_dom.php';
$html = file_get_html('http://www.weather.gov.sg/lws/zoneInfo.do');

$n = 0;
$table = $html->find('table',3)->find('table',0)->find('table',0)->find('table',0)->find('table',3)->find('table',0);

$i = -3;
$rows = $table->find('tr');
$holder = array();

foreach($rows as $element){
    $i++;
    if($i < 0) continue;

    $holder[$i]['name'] = $element->find('td',0)->plaintext;
    $holder[$i]['zone_or_school'] = $element->find('td',1)->plaintext;
    $holder[$i]['risk'] = $element->find('td',2)->plaintext;
    $holder[$i]['from'] = $element->find('td',3)->plaintext;
    $holder[$i]['till'] = $element->find('td',4)->plaintext;
}

var_dump($holder);
?>

if you want to get a particular data then you can filter it out:

foreach($holder as $key => $val)
{
if($holder[$key]['name']=='Bedoc')
$my_data = $holder[$key];
}

this code isn't debuged cause i am on mobile now. But maybe you have get the idea if not works. Thanks

share|improve this answer
    
Thanks for your kind input. I didin't expect you to write the whole thing out for me. However, I actually want to explore the possibility of using a method to find 'Bedok' instead of counting the line it is in. There are 400+ lines there. –  user2216416 Apr 1 '13 at 17:35
    
@user2216416: edited my answer. Try this. Let me know if it is ok or not. –  user2193789 Apr 1 '13 at 17:47
    
Wow, just awesome! Thanks a lot! Its so much efficient than my code. No need to wait for 2 mins for everything to show. Wow! –  user2216416 Apr 2 '13 at 1:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.