This is my xml file

<CATALOG>
	<category>
		<name>Engineering</name>
		<subject>Civil Works</subject>
      <subject>Water Supply</subject>
      <subject>Street Lighting</subject>
	</category>

<category>
		<name>Public Health</name>
		<subject>Sanitation</subject>
      <subject>Dispensaries</subject>
      <subject>Maternity Services</subject>
	</category>

	<category>
		<name>Town Planing</name>
		<subject>T.P.S</subject>
		<subject>T.P.B.O</subject>
      <subject>Tracer</subject>
      <subject>Chairmen</subject>
	</category>

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

</CATALOG>

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

<?php

     
		 $xml = simplexml_load_file("../category.xml");
        
$subjects = 'Hello';
$categoryes=$xml->xpath('/category');
			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.

Hi,

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.

<?php
$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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.