Jump to content

DomDocument; hasChildNodes but Foreach not showing children?


redgenieuk
Go to solution Solved by redgenieuk,

Recommended Posts

Morning! I have started using DOMdocument for a job but hit an issue with this code snippet.

 

 

     if ($txtnode1->parentNode->hasChildNodes()) {
                                Echo "***Has Children Start***<br>";
                                foreach ($txt1->parentNode as $child) {
                                    Echo "NodeChild<br>";        
                                }
                                Echo "***Has Children End***<br>";
      }

I get the output:

***Has Children Start***
***Has Children End***

So 

($txtnode1->parentNode->hasChildNodes())

is true;

But

foreach ($txtnode1->parentNode as $child)

Isn't looping.... I think indicating  there are no children?

But there must be a child.... because we are going to $txtnode1's parentNode... so in summary i am very confused!

Any help greatly appreciated. TIA.

Link to comment
Share on other sites

  • Solution

Stupidity has been found. I am answering my own question as I cannot see how to remove it; but hopefully it might help someone else one day!

Answer is:

foreach ($txtnode1->parentNode as $child)

should be

foreach ($txtnode1->parentNode->childNodes as $child)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.