1

I am using simple_html_dom to grab links from a url but I can't solve this problem i am having.

    <?php

        $title_name_lower = strtolower($title_name);

        $file_headers = @get_headers($title_name_lower);

            if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
                 echo '<center><strong style="color: #dd3333;">No links found :( </strong><center>';
            }

            else {

                $title_url = file_get_html(''.$title_name_lower.'');

                $items = array();
                $items_text = array();
                foreach($title_url->find('table[class=alternate_color variousfont] tr td a') as $item)  {
                 $items[] = $item->href.'<br>';
                 $items_text[] = $item->plaintext.'<br>';
                }

                //print_r($items_text);
                //print_r($items);

                $m_title_1 = file_get_html(''.$items[3].'');
                $m_title_2 = file_get_html(''.$items[6].'');
                $m_title_3 = file_get_html(''.$items[9].'');
                $m_title_4 = file_get_html(''.$items[12].'');
                $m_title_5 = file_get_html(''.$items[15].'');
                $m_title_6 = file_get_html(''.$items[27].'');
                $m_title_7 = file_get_html(''.$items[30].'');

                $link_1 = $m_title_1->find('div[class=post-single-content box mark-links] a', 0)->href;
                $link_2 = $m_title_2->find('div[class=post-single-content box mark-links] a', 0)->href;
                $link_3 = $m_title_3->find('div[class=post-single-content box mark-links] a', 0)->href;
                $link_4 = $m_title_4->find('div[class=post-single-content box mark-links] a', 0)->href;
                $link_5 = $m_title_5->find('div[class=post-single-content box mark-links] a', 0)->href;
                $link_6 = $m_title_6->find('div[class=post-single-content box mark-links] a', 0)->href;
                $link_7 = $m_title_7->find('div[class=post-single-content box mark-links] a', 0)->href;
            }

        echo $link_1.' - '.$items_text[3];
        echo $link_5.' - '.$items_text[15];
        echo $link_7.' - '.$items_text[30];     
    ?>

The $items variable can be 100 links can be only 4 and if $items is for example 50 links the $items_text is same (50).

Now when I echo $link_1, $link_5, $link_7, in some pages it works but in some it shows an error:

Call to a member function find() on a non-object

I don't want to show any error. I want if the variable is empty than don't show anything.

1 Answer 1

1

You need to do something like this i think.

if (is_object($m_title_7)) {
    $link_7 = $m_title_7->find('div[class=post-single-content box mark-links] a', 0)->href;
} else {
    $link_7 = "";
}

This will check to see if $m_title_7 is an object first and if it is then it will set $link_7.

edit added else to empty $link_7

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.