PHP XML foreach loop help

I have an XML file in which I need to loop through and get certain dat

Page 1 of 1

6 Replies - 7685 Views - Last Post: 31 December 2009 - 04:34 PM Rate Topic: -----

#1 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

PHP XML foreach loop help

Posted 30 December 2009 - 02:34 PM

Hello,

I have an XML file similar to this:

<list>
<ac val="505">
<ph val="1234567" />
<ph val="2345678" />
<ph val="3456789" />
</ac>
</list>



I have tried to run it through a foreach loop to get each of the ph attributes (val) displayed.
I have tried numerous ways but this is my most recent fail:

<?php

// set name of XML file
$file = "donotcall.xml";

// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");

// access XML data
foreach($xml->ac->ph as $i)
{
	echo "Number:  " . $xml->ac->ph[$i]->attributes() . "\n<br />";
}



Thanks for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: PHP XML foreach loop help

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: PHP XML foreach loop help

Posted 30 December 2009 - 02:40 PM

// access XML data                                                              
$nums = $xml->xpath("/list/ac/ph");
foreach($nums as $num)
{
    echo "Number:  " . $num->attributes() . "\n<br />";
}


Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: PHP XML foreach loop help

Posted 30 December 2009 - 03:42 PM

Yay, a server side language.

Right, this should get you going:
<?php
function getXml() {
	$fileName = "donotcall.xml";
	$xml = simplexml_load_file($fileName) or die ("Unable to load XML file!");
	return $xml;
}

function dumpXml() { 
	$xml = getXml();
	echo '<pre>';
	print_r($xml);
	echo '</pre>';
}

function dumpNums() { 
	$xml = getXml();
	echo "<ul>";
	foreach($xml->ac->ph as $item) {
		echo '<li>' . $item->attributes()->val . "</li>\n";
	}
	echo "</ul>";
}

?>
<html>
<head>
	<title>XML Test</title>
</head>
<body>
	<h1>XML Test</h1>
	<? dumpNums(); ?>
</body>
</html>



Note the dumpXml(). That should be your first stop, just to get a feel for what your data looks like.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: PHP XML foreach loop help

Posted 30 December 2009 - 05:18 PM

Ooops...sorry, I see XML I immediately think XPath.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: PHP XML foreach loop help

Posted 30 December 2009 - 05:34 PM

View PostJackOfAllTrades, on 30 Dec, 2009 - 06:18 PM, said:

Ooops...sorry, I see XML I immediately think XPath.


Lol, same. However, people seem to have a mental block against it. Also, there's a little bit of overhead. If the path is just enumerating basic elements it's not a bad idea to just to pick it up manually.

To the OP, if the goal is the search for a specific element, XPath is a better choice than loops. e.g.
$elements = $xml->xpath("/list/ac[@val='$acVar']/ph[@val='$phVar']");


Was This Post Helpful? 0
  • +
  • -

#6 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: PHP XML foreach loop help

Posted 31 December 2009 - 08:25 AM

OK thanks guys.

Baavgai you say that if I am looking for a specific element to just use XPath (because that is exactly what I need to do, grab one element)? Will doing that in PHP be faster than the way we were doing it Javascript, even without a DB setup?

Thanks
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: PHP XML foreach loop help

Posted 31 December 2009 - 04:34 PM

View PostMoshambi, on 31 Dec, 2009 - 09:25 AM, said:

Will doing that in PHP be faster than the way we were doing it Javascript, even without a DB setup?


Probably. Here's a simple test:
<?php
function getXml() { return simplexml_load_file("donotcall.xml"); }

function processRequest() {
	if(isset($_GET['txtPhone'])) {
		$ph = $_GET['txtPhone'];
		$phLen = strlen($ph);
		if ($phLen>0) {
			echo "<p>Number: $ph<br/>\n";
			if ($phLen==7) {
				$xml = getXml();
				$elements = $xml->xpath("/list/ac/ph[@val='$ph']");
				if (count($elements)>0) {
					$src = "donotcall.bmp";
					$alt = "Do Not Call";
				} else {
					$src = "oktocall.bmp";
					$alt = "Ok to Call";
				}
				echo '<img id="resultImg" src="' . $src . '" alt="' . $alt . '"/>';
			} else {
				echo "Invalid Number";
			}
			echo "</p>\n";
		}
	}
}
?>
<html>
<head>
	<title></title>
</head>
<body>
	<h1>Lookup Test</h1>
	<form method="GET">
		<input type="text" name="txtPhone" value="" />
		<input type="submit" value="Check Number">
	</form>
	<div id="result"><? processRequest(); ?></div>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1