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

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


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:
      <customer id="000">
                  <date dt="29/11/2008">
                        <payDate>new acc</payDate>
                        <amtPaid> </amtPaid>
                  <date dt="28/02/2009">
                  <date dt="29/05/2009">
                  <date dt="29/08/2009">

<?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 />";
// ......
<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"/>
<p><span style="color:red;"><?php echo $error; ?></span></p>

Open in new window

Who is Participating?

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

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');
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:


So the error likely refers to the fact you're trying to call a method which does not exist for a DOMNode object.
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


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

From novice to tech pro — start learning today.