tags:

views:

37

answers:

3

Hello all,

I was just helped with this question but I can't get it to move to the next block of HTML.

$html = file_get_html('http://music.banadir24.com/singer/aasha_abdoo/247.html');

$urls = $html->find('table[width=100%] table tr');

foreach($urls as $url){

     $song_name = $url->children(2)->plaintext;

     $url = $url->children(6)->children(0)->href;   

}

It returns the list of the names of the first album (Deesco) but it does not continue to the next album (The Best Of Aasha)? It just gives me this error:

Notice: Trying to get property of non-object in C:\wamp\www\test3.php on line 26

Fatal error: Call to a member function children() on a non-object in C:\wamp\www\test3.php on line 28

Why is this and how can I get it to continue to the next table element?

I appreciate any help on this!

Please note: This is legal as the songs are not bound by copyright and they are available to download freely, its just I need to download a lot of them and I can't sit there clicking a button all day. Having said that, its taken me an hour to get this far.

A: 

The error means that your query for ->children(6) did not return a valid object - presumably because there is no sixth child in that row.

To find out why not, try to debug the returned HTML structure - e.g. using print_r($url).

Pekka
@Doing that now, its taking ages to `print_r($url)`. Done now, there are loads of elements returned, scroll bar is tiny, crashed the browser! Why is there so much, isn't my select narrowing things down? Is there another selector I can use to narrow things down and include the second section?
*print_r()* is huge certainly because the node references its parent, the parent its own parent, and so on...
mexique1
A: 

i think you'll want to refine your selector to include only the tr's of the album names. currently i believe you'll be grabbing all tr's, which could have unexpected results. maybe something like $html->find('table[width=100%] table tr td .title');

nathan gonzalez
@Nathan - this selector doesn't seem to return anything?
sorry, didn't test it out. the basic idea though would be to create a selector that only grabs the row of the table that contains a td with a class of title, as that is what is found in the album name tr
nathan gonzalez
A: 

Maybe you'd better write it like this :

foreach($urls as $url){

    if ($node = $url->children(6)) {
        $node->children(0)->href;   
    }

}
mexique1
Ah, that did the trick! Thanks. :)
I have just added error control ! BTW, you should also check if *$node->children(0)* returned something.
mexique1
@Mexique1 - thank you!