getting Last Item from XML Node

hi I'm looping thru a xml and all the information output is correct but now i want to get a the the last element value of <reading> not each one.
 XML CODE:
<customers>
      <customer id="000">
            <meter>123</meter>
            <name>
                  <title/>
                  <firstName>May</firstName>
                  <middleName/>
                  <lastName>Smith</lastName>
            </name>
            <dates>
                  <date dt="29/11/2008">
                        <reading>62990</reading>
                        <amtDue>0.00</amtDue>
                        <payDate>new acc</payDate>
                        <amtPaid/>
                  </date>
                  <date dt="28/02/2009">
                        <reading>64492</reading>
                        <amtDue>242.21</amtDue>
                        <payDate>30/12/2000</payDate>
                        <amtPaid>5</amtPaid>
                  </date>
                  <date dt="29/05/2009">
                        <reading>65374</reading>
                        <amtDue>157.23</amtDue>
                        <payDate>3012200</payDate>
                        <amtPaid>5</amtPaid>
                  </date>
                  <date dt="29/08/2009">
                                                                ****<reading>66348</reading>***//want to get this value
                        <amtDue>170.60</amtDue>
                        <payDate>6</payDate>
                        <amtPaid>6</amtPaid>
                  </date>
            </dates>
      </customer>
</customers>

$doc = new DomDocument;
				$doc->Load('files/customers.xml') or die('Unable to load file');
				$count = 0;
				$i = 0;
				
					 $node = $doc->getElementsByTagName('customer');
						foreach( $node as $find ){
							$customerMeters = $find->getElementsByTagName('meter');
							
								foreach($customerMeters as $customerMeter){
								$meter = $customerMeter->nodeValue;
							
									if ($mnum == $meter){
										$fnames = $doc->getElementsByTagName('firstName');
										$fname = $fnames->item($count)->nodeValue;
										$lnames = $doc->getElementsByTagName('lastName');
										$lname = $lnames->item($count)->nodeValue;	
										echo "Updating Meter reading and billing for <b>".$fname." ".$lname."</b>";
										// *************************
										
										$datesNodes = $find->getElementsByTagName('dates');
											foreach ($datesNodes as $datesNode) { 
												$dates = $datesNode->getElementsByTagName('date'); 
												
													foreach ($dates as $date) { 
														$readings = $date->getElementsByTagName("reading")->item(last)->nodeValue;
														echo "<br />".$readings;
													}// foreach
											$i++;
											}//foreach 
																	
										//***************************
										
									}
								$count++;	
								}// end foreach meter
						
						}

Open in new window

LVL 4
bobby101Asked:
Who is Participating?
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:
oh sorry this is coming from a form as attached
<form name="reading" method="post" action="">
<table cellspacing="3">
	<tr><td align="right">Date:</td><td><input type="text" name="date" size="20" /> DD/MM/YYYY</td></tr>
	<tr><td align="right">Meter Number:</td><td><input type="text" name="mnum" size="20" /></td></tr>
	<tr><td align="right">Meter Reading:</td><td><input type="text" name="readings" size="20" /></td></tr>
	<tr><td>&nbsp;</td><td><input type="submit" value="Add" name="add" /></td></tr>
	<tr><td>&nbsp;</td><td><p><span style="color:red;"><?php echo $error; ?></span></p></td></tr>
</table>
</form>

Open in new window

0
 
cyberkiwiCommented:
When you exit this loop, $reading has the last value

foreach ($dates as $date) {
    $readings = $date->getElementsByTagName("reading")->item(last)->nodeValue;
    //echo "<br />".$readings;
}// foreach
0
 
Shinesh PremrajanEngineering ManagerCommented:
I am not sure with the code, its worth giving a try ..


Just get the count of the dates in the xml

$dateCount= count($date->item);

OR

$dateCount= count($date);

$dateCount=$dateCount-1; // as it index starts with 0

then use the date count in place of the last variable  in your code.
like given below

$readings = $date->getElementsByTagName("reading")->item($dateCount)->nodeValue;


Hope this helps.

shinuq
                                                                                                                 
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
bobby101Author Commented:
$readings = $date->getElementsByTagName("reading")->item(last)->nodeValue;

error message:
Warning: DOMNodeList::item() expects parameter 1 to be long, string given in C:\xampplite\htdocs\dwax\processmeter.php
0
 
cyberkiwiCommented:
I see, you can just give it 0

$readings = $date->getElementsByTagName("reading")->item(0)->nodeValue;

Then when the loops end, $readings contains the value you want.
0
 
bobby101Author Commented:
item(0) bring the first value
0
 
cyberkiwiCommented:
Have you tried this?

foreach ($dates as $date) {
    $readings = $date->getElementsByTagName("reading")->item(0)->nodeValue;
}// foreach
echo "<br />".$readings;

echo the result only after you exit the loop - it will be the last reading
0

Experts Exchange Solution brought to you by ConnectWise

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
 
bobby101Author Commented:
THANKS A+
0
 
StealthyDevCommented:
I think you can do it as below:

$readings = $dates[$dates.length - 1]->getElementsByTagName("reading")->item(0)->nodeValue;
echo "<br />".$readings;
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.