This is my xml file

		<subject>Civil Works</subject>
      <subject>Water Supply</subject>
      <subject>Street Lighting</subject>

		<name>Public Health</name>
      <subject>Maternity Services</subject>

		<name>Town Planing</name>

		<subject>Ministerial Manager</subject>
      <subject>Revenue R.O.</subject>
      <subject>Accounts Accountant</subject>


I have to get the all subject names in category spesified by $_GET["category"];
and i wrote the code as follows.


		 $xml = simplexml_load_file("../category.xml");
$subjects = 'Hello';
			foreach($categoryes as $name)
				echo $name;
				if($name == $_GET["category"] )
					foreach($xml->chaildren() as $child)
						$subjects = $subjects." ".$child;

		echo $subjects;

what wrong with this code?

thank you.

Edited by sagarrayudu: n/a

5 Years
Discussion Span
Last Post by veedeoo


It is getting really late in my time zone, that I really need to hit the bunk. Anyways, here is the general form for xpath in php..

I am not going to explain it in great detail, but the whole idea is well presented.

$xml = simplexml_load_file("../category.xml");

$cat_name = $_GET['category'];

## uncomment $cat_name below to test

//$cat_name = "Civil Works";

$result = $xml->xpath("category[subject='".$cat_name."']/name");

foreach($result as $name){

## echo the name of the matched
echo $name."<br/>";

If run the script above, it should give you "Engineering" as a match from the nodes. Just expand the script above to your requirements.

Edited by veedeoo: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.