Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7057005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:56:04+00:00 2026-05-28T03:56:04+00:00

I have this program with a switch: $htmlContent = file_get_contents(‘http://somesite.com’); $htmlDOM->loadHTML( $htmlContent ); $htmlXPath

  • 0

I have this program with a switch:

$htmlContent = file_get_contents('http://somesite.com');
$htmlDOM->loadHTML( $htmlContent );
$htmlXPath = new DOMXPath( $htmlDOM );

for($i = 0; $i <= 16; $i++ ) {
    switch($i) {
        case 0:
            $link = utf8_decode($htmlXPath->query('/html/body/div[2]/div[2]/div/div/div/div/div[2]/div/a')->item(0)->getAttribute('href'));
            break;
        case 1:
            $link = utf8_decode($htmlXPath->query('/html/body/div[2]/div[2]/div/div/div/div/ul/li/div/a')->item(0)->getAttribute('href'));
            break;
        default:
            $link = utf8_decode($htmlXPath->query('/html/body/div[2]/div[2]/div/div/div/div/ul/li[' . $i . ']/div/a')->item(0)->getAttribute('href'));
            break;
    }
}

For case 0 and case 1 work like expected, but with default is throwed this error:

PHP Fatal error:  Call to a member function getAttribute() on a non-object

I can imagine that it occur because of $i, but how can I solve this problem?

Thanks for help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:56:05+00:00Added an answer on May 28, 2026 at 3:56 am

    Most likely the query’s failing to find anything and returning a zero-length node list. Attempting to fetch a node from an empty list returns false, not an object.

    Instead of assuming the query succeeded, use an intermediate holder to check:

    $nodes = $htmlXPath->query('/html/body/div[2]/div[2]/div/div/div/div/div[2]/div/a');
    if ($nodes->length > 0) {
       $link = $nodes->item(0)->getAttribute('href'));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this source in flex - easy program which switch between two states..in
I have this program that should execute a piece of code base on the
I have this program, but cin in randomly skips.. I mean sometimes it does,
I have this program that I am working on for class, I think the
note: I am sorry if this is a stupid question! I have this program
I have this function: Program A public ICollection<ReportLocationInfo> GetAllReportsInstalled() { return _Reports; } I
I have this servlet program and an external .jar file (written by someone else)
I have this C++ program: int box[9][9]; box = { {solution[0][0],solution[0][1],solution[0][2],solution[1][0],solution[1][1],solution[1][2],solution[2][0],solution[2][1],solution[2][2]}, {solution[0][3],solution[0][4],solution[0][5],solution[1][3],solution[1][4],solution[1][5],solution[2][3],solution[2][4],solution[2][5]}, {solution[0][6],solution[0][7],solution[0][8],solution[1][6],solution[1][7],solution[1][8],solution[2][6],solution[2][7],solution[2][8]}, {solution[3][0],solution[3][1],solution[3][2],solution[4][0],solution[4][1],solution[4][2],solution[5][0],solution[5][1],solution[5][2]},
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I have run this program before and it worked fine. Then I added the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.