Search code examples
phpsimple-html-dom

How to detect element is exist or not


Hello everyone i am fetching data by using simple html dom

This is my code of php which is fetching data from site include('simple_html_dom.php');

$html = new simple_html_dom();
$html->load_file($this->main_url.$lin->link);
if($html){
    //check if language heading h2 exist then process forward
    if($html->find('h2.channel-title',0)){
        fetch data from tables
     }
}

This line if($html->find('h2.channel-title',0)) finding h2.channel-title in find function of simple html dom give me a fatal error when h2.channer-title is not exist

In many pages <h2 class="channel-title"> English Links</h2> exists so i have code according to them and process further in my foreach loop it's working fine and fetched all data.

But

when <h2 class="channel-title">English Links</h2> tag is not exist it give me an error

Fatal error: Call to a member function find() on a non-object in C:\xampp\apps\wordpress\htdocs\wp-content\plugins\autobot\engine\simple_html_dom.php on line 1113

Please help me i am stuck in it need help thank you. i want if h2.channel-title exist run my foreach code else run another but don't give an error its stop my whole script. :(


Solution

  • this might help.

    $html = new simple_html_dom();
    $html->load_file($this->main_url.$lin->link);
    
    if($html) {
        $var = $html->find('h2.channel-title',0);
    
        if(isset($var)) {
            fetch data from tables
        } else{
            //do something
        }
    }