Search code examples
phpparsinghtml-parsingsimple-html-domhtml-parser

PHP Simple HTML DOM Parser - loop


I started to play Simple HTML Doom Parser and I have some trouble:

The HTML code is as follows:

<div class="players">
    <ul class="dane">
        <li>
            <div class="name">Messi</div>
            <div class="value">Barcelona</div>
        </li>
        <li>
            <div class="name">Ronaldo</div>
            <div class="value">Madryt</div>
        </li>
    </ul>
</div>

<div class="clubs">
    <ul class="dane">
        <li>
            <div class="name">Barcelona</div>
            <div class="value">Hiszpania</div>
        </li>
        <li>
            <div class="name">Madryt</div>
            <div class="value">Hiszpania</div>
        </li>
    </ul>
</div>

<div class="trenerzy">
    <ul class="dane">
        <li>
            <div class="name">Ernesto Valverde</div>
            <div class="value">Barcelona</div>
        </li>
        <li>
            <div class="name">Madryt</div>
            <div class="value">Zidane</div>
        </li>
    </ul>
</div>

My PHP code:

include_once('simple_html_dom.php');
$html = file_get_html('http://');

$articles = array();
foreach($html->find('ul.dane') as $article) {
    $item['name']     = $article->find('div.name',0)->plaintext;
    $item['value']    = $article->find('div.value',0)->plaintext;
    $articles[] = $item;
}

print_r($articles);

The data it receives is:

Array
(
    [0] => Array
        (
            [name] =>                   Messi               
            [value] =>                      Barcelona               
        )

    [1] => Array
        (
            [name] =>                   Barcelona               
            [value] =>                      Hiszpania               
        )

    [2] => Array
        (
            [name] =>                   Ernesto Valverde                
            [value] =>                      Barcelona               
        )

It receives data from the data class ul.dane but only 1 record, how can I do to get everything that is on the page?

I know I'm missing a loop somewhere, but I have a problem finding wher


Solution

  • You probably want to

    1. Find all ul.dane elements
    2. Inside each ul, search for li elements
    3. Inside each li, search for div.name and div.value elements

    In that case the problem with your code is that you forgot to find each li element inside each ul, which would be step 2. Try this:

    foreach($html->find('ul.dane') as $ul) {
        foreach($ul->find('li') as $article){
            $item['name']     = $article->find('div.name',0)->plaintext;
            $item['value']    = $article->find('div.value',0)->plaintext;
            $articles[] = $item;
        }
    }