N
N
Nikolai Kovalevsky2017-05-02 13:45:50
PHP
Nikolai Kovalevsky, 2017-05-02 13:45:50

Why doesn't the Simple HTML DOM function work in a loop?

Such a problem, I copy the list of streams from the site through SimpleHTMLDOM. The problem is that the list is placed on several category pages. Through foreach, I load several categories, but starting from the second iteration, the simple HTML DOM functions do not work.

<?php
include "simple_html_dom.php";
function GetRWGames($rw_cat){
    $live = new simple_html_dom();
    $live->load_file("http://live.robinwidget.org/static/section" . $rw_cat . ".html");
    $data = $live->find(".lshpanel");
    $counter = 0;
    foreach($data as $game) {
        $games[$counter][cat] = $game->find(".section",0)->plaintext;
        $games[$counter][time] = $game->find(".date",0)->plaintext;
        $temp = $game->find(".lshevent",0)->plaintext;
        $temp = explode(" - ", $temp);
        $games[$counter][team1] = trim ($temp[0]);
        $games[$counter][team2] = trim ($temp[1]);
        $temp = $game->find("a");
        foreach ($temp as $a) {
            $a=$a->href;
            if(stristr($a, "javascript:openWindow")==true){
                $temp= strstr($a, "http");
                $temp= strstr($temp, '"',1);
                $games[$counter][links] .= $temp . ";";}
            elseif(!strpos($a,"adserving")&&!strpos($a,"adsrv")&&!empty($a))
            {$games[$counter][links] .= $a . ";";}
        }
        $counter++;
    }
    return $games;
}
$games = array();
// Load HTML from a string
$rw_cats = array(35,36,37,38,39,57,59);
foreach($rw_cats as $rw_cat){
print_r(GetRWGames($rw_cat));
}

Mistake:
Fatal error: Uncaught Error: Call to a member function find() on null in D:\os\OpenServer\domains\newtest.ru\simple_html_dom.php:1113 Stack trace: #0 D:\os\OpenServer\domains\newtest.ru\index.php(7): simple_html_dom->find('.lshpanel') #1 D:\os\OpenServer\domains\newtest.ru\index.php(34): GetRWGames(36) #2 {main} thrown in D:\os\OpenServer\domains\newtest.ru\simple_html_dom.php on line 1113

The first iteration is successful, I get the data. In the second iteration, find stops working. At the same time, the pages seem to be loading, I checked it simply by loading and outputting. I tried to generate the $live name, manually provide the simple_html_dom object for processing in $data, did it without loops at all, manually sorting through the iterations. Constantly an error on the first find.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vit134, 2017-05-02
@Silert

almost the same problem Call to a member function innertext() on null

function getContent($url) {
    $html = new simple_html_dom();
    $html->load_file($url);
    $html->save();

    $table = $html->find('table.tb-matches');

    foreach ($table as $key => $value) {
       foreach ($value->find('tr') as $key => $tr) {
            $elem =  $tr->find('td', 0);
            echo 'key' . $key . '<br>';
            echo 'tr' . $tr . '<br>';
            echo $tr->find('td', 0)->attr['colspan'] . '----<br>';
        }
    }

}
foreach ($dateArr as $key => $dateVal) { 
    getContent($url . $dateVal); 
}

tried after foreach ($table as $key => $value) { add $value = new simple_html_dom();
the error began to appear immediately, even the first iteration is not performed
solved the problem like this: first I add the content of all pages to the variable
foreach ($dateArr as $key => $dateVal) {
        $out .=  file_get_contents($url . $dateVal) ;
}

and then I create a simple_html_dom object
$html = new simple_html_dom();
    $html->load($out);
    $html->save();

    $table = $html->find('.list_table');

    foreach ($table as $key => $value) {
        $dd = $value->attr['date'];

        foreach ($value->find('table.tb-matches tr') as $key => $tr) {

            $elem =  $tr->find('td', 0);

            if ($elem->attr['colspan'] != '7') {
                $resultArr[$dd][] = array(
                    'time' => $tr->find('td', 0)->plaintext,
                    'home_team' => $tr->find('td.home-team', 0)->plaintext,
                    'away_team' => $tr->find('td.away_team', 0)->plaintext,
                    'status' => trim($tr->find('td.status', 0)->plaintext),
                    'score' => $tr->find('td.score', 0)->plaintext,
                    'href' => 'http://goal24.ru' . $tr->find('td.option a', 0)->href,
                );
            }
        }
    }

Found another solution
in the function that runs in a loop, replace $html = new simple_html_dom() with $html = str_get_html(file_get_contents($url)) . Result on the face.

Y
Yurka Blokhin, 2017-05-02
@blrik

Add $game = new simple_html_dom(); after foreach($data as $game)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question