0

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.

4
  • what data you are expecting? Commented Apr 1, 2013 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 Commented Apr 1, 2013 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? Commented Apr 1, 2013 at 10:49
  • I want the data on the right of the specific area mentioned, e.g Bedok, Zone, -, - in an array. Commented Apr 1, 2013 at 10:55

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

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: edited my answer. Try this. Let me know if it is ok or not.
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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.