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

OK
so it showing the info but stops continuing futher!!!!! and shows the error message!!!!!
---------------------------------------------------------------------------------------------------
this is the browser output:

29/11/2008
28/02/2009
29/05/2009
29/08/2009

Fatal error: Call to a member function getAttribute() on a non-object in C:\xampplite\htdocs\dwax\billpayment.php on line 24
---------------------------------------------------------------------------------------------------

here is my XML CODE:
<customers>
      <customer id="000">
            <name>
                  <title/>
                  <firstName>Bart</firstName>
                  <middleName/>
                  <lastName>Simpson</lastName>
            </name>
            <dates>
                  <date dt="29/11/2008">
                        <reading>62990</reading>
                        <amtDue>0.00</amtDue>
                        <payDate>new acc</payDate>
                        <amtPaid> </amtPaid>
                  </date>
                  <date dt="28/02/2009">
                        <reading>64492</reading>
                        <amtDue>242.21</amtDue>
                        <payDate>30/03/2009</payDate>
                        <amtPaid>242.21</amtPaid>
                  </date>
                  <date dt="29/05/2009">
                        <reading>65374</reading>
                        <amtDue>157.23</amtDue>
                        <payDate>24/06/2009</payDate>
                        <amtPaid>157.23</amtPaid>
                  </date>
                  <date dt="29/08/2009">
                        <reading>66348</reading>
                        <amtDue>170.60</amtDue>
                        <payDate>19/09/2009</payDate>
                        <amtPaid>170.60</amtPaid>
                  </date>
            </dates>
      </customer>
</customers>

<?php if (array_key_exists('search', $_POST)) {  
	$target = $_POST['target']; 
	if(!$target == ""){
		$count = 0;
        $file = "files/customers.xml"; 
		  
			  $doc = new DOMDocument();
			  $doc->load($file) or die('Unable to load file');
			  
			  $node = $doc->getElementsByTagName('customer');
			  foreach( $node as $find ){
			  $id = $find->getAttribute('id');
			 
					  if ($target == $id){
						$num = 0;
						$d = $doc->getElementsByTagName('dates')->item($count);
						while ($d){
						   $date = $d->getElementsByTagName('date')->item($num)->getAttribute('dt');
						   echo $date."<br />";
						   $num++; 
						   ;
						}
						
					  }
				$count++;
			  }
	}
}
// ......
?>
<form name="search" method="post" action=""> 
<h3>Bill Payment</h3>
<p>Customer ID: <input type="text" name="target" size="20" /></p>
<input type="submit" value="Go" name="search" /><input type="hidden" name="search" value="1"/>
</form></p> 
<p><span style="color:red;"><?php echo $error; ?></span></p>

Open in new window

LVL 4
bobby101Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bobby101Author Commented:

this line is causing the error
$date = $d->getElementsByTagName('date')->item($num)->getAttribute('dt');
0
HackneyCabCommented:
According to the PHP manual pages for the DOM classes, the item($num) part will return an object of type DOMNode, or null if the $num index is invalid (refers to an element which does not exist).

The DOMNode class does not have a method (that I can see) called getAttribute, according to its manual page:

http://php.net/manual/en/class.domnode.php

So the error likely refers to the fact you're trying to call a method which does not exist for a DOMNode object.
0
rjdownCommented:
Sure it does: http://php.net/manual/en/domelement.getattribute.php

The problem is with your logic in looping through the elements. In particular, line 17:

while ($d) {

that makes no sense, as $d is defined outside the loop and is never changed inside it. So what's happening is that $num just continues to increase, until it reaches the stage where $d->getElementsByTagName('date')->item($num) doesn't exist. Obviously you can't call a function on something that doesn't exist, hence the fatal error.

Saying that, there really is no need to keep counters anyway; you can loop through nodes and their children using foreach, like you're doing with the customers. IMO it makes things a lot easier to understand when you come back to it.

Try this:
if (isset($_POST['target']) && $_POST['target'] != "") {

	$target = $_POST['target'];

	$file = "files/customers.xml";
	$doc = new DOMDocument();
	$doc->load($file) or die('Unable to load file');

	$customerNodes = $doc->getElementsByTagName('customer');
	foreach( $customerNodes as $customerNode ) {
		$id = $customerNode->getAttribute('id');
		if ($target == $id) {
			$datesNodes = $customerNode->getElementsByTagName('dates');
			foreach ($datesNodes as $datesNode) {
				$dateNodes = $datesNode->getElementsByTagName('date');
				foreach ($dateNodes as $dateNode) {
					echo $dateNode->getAttribute('dt') . "<br />";
				}
			}
		}
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.