Looping through XML file

Having problem loop through nodes
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:
XML CODE

<customers>
      <customer id="000">
            <meter>134456</meter>
            <name>
                  <title/>
                  <firstName>Bart</firstName>
                  <middleName/>
                  <lastName>Simpson</lastName>
            </name>
            <address>
                  <street>45 Olive Dr</street>
                  <city>Sydney</city>
                  <postcode>2000</postcode>
                  <state>NSW</state>
            </address>
            <phone>
                  <home>0298576433</home>
                  <mobile>0413335678</mobile>
                  <work>0296784455</work>
            </phone>
            <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>


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

<?PHP 
session_start();
if (!$_SESSION['username']){
	header('Location: login.php');
}
$error = "";
	$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);
						   $dt = $date->getAttribute('dt');
						   echo $dt."<br />";
						   $num++; 
						}
						
					  }
				$count++;
			  }
	}
	//else if ($target == ""){$error="Unsuccessful";}	
		
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Energetic Energy Management</title>
</head>
<body>
	<h3>Welcome, <?php echo $_SESSION['username']; ?></h3>
	<a href="logout.php">Logout</a>
	<p>&raquo; <a href="index.php">Home</a>

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

Open in new window

0
Avinash ZalaWeb ExpertCommented:
Try attached code with your xml file, change the xml file name accordingly.

Hope this helps,
Addy
<?php 
    $webroot="http://".$_SERVER['HTTP_HOST']."/";
    $doc = new DOMDocument();

    $doc->load( $webroot.'/demo.xml' );

    $note = $doc->getElementsByTagName( "customer" );
    $final_arr = array();
    $i=0;
    foreach($note as $note)
    {
		$id = $note->getAttribute('id');

		$dates = $note->getElementsByTagName("dates");
	
        foreach($dates as $dates)
        {
			$date = $dates->getElementsByTagName("date");
			foreach($date as $date)
			{
				echo  $dated= $date->getAttribute('dt');
				 echo "<br/>";
			}
        }
        $i++;
    }
?>

Open in new window

0
bobby101Author Commented:
Sorry didnt work i its bring up the dates from other customer/s nodes as well.
basically how it should work is that you type in the customer id and brings up the billing date history of that customer i had it working but it also gives a error:
Fatal error: Call to a member function getAttribute() on a non-object in C:\xampp\htdocs\dwax\billpayment.php on line 24

0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

bobby101Author Commented:
output on browser:
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
0
Avinash ZalaWeb ExpertCommented:
i am not getting any error at all.

and which dates you want to get?


Addy
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
Web Components

From novice to tech pro — start learning today.