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?
 
rjdownConnect With a Mentor Commented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.