Dear friends of daniweb.com;
An annoying problem has been arisen in muy php code. Here is the code :

$doc = new DOMDocument();
    $doc->load("http://localhost/xampp/tsbs_yellow_pages/run/databank.xml");
    $xPath = new DOMXPath($doc);
    $nodeList = $xPath->query("/Root/User");
    //Everything is normal up to that point I think

	$phase;
	$length_of_phase;
	$areaPointer=99;
	$i=0;
		
	//In these "if" blocks , I try to determine which area has been filled by user.

/*
    $in_name = $_POST["name"]; 
    $in_surname = $_POST["surname"];
    $in_department = $_POST["department"];
has been declared before. 

*/         

	if($in_name != '')
	{
		$areaPointer = 1;
		$phase = trim(($in_name));
		$length_of_phase = strlen($phase);
	
				
	}else if($in_surname != '')
	{
		$areaPointer = 2;
		$phase = trim($in_surname);
		$length_of_phase = strlen($phase);
		
	}else if($in_department != '')
	{
		$areaPointer = 3;
		$phase = trim($in_department);
		$length_of_phase = strlen($phase);
	
	}
		
	for( $i=0 ; $i<($nodeList->length)-1 ; $i++)
	{

		switch($areaPointer)
		{
		
			case 1:
				echo "here";//Next line is the error point. 
if( strncasecmp($nodeList->item[$i]->getElementsByTagName('name')->item[0]->nodeValue,$phase,$length_of_phase) == 0 )
				{
					echo("<p>$$nodeList[$i]->nodeValue</p>\n");
				}
			break;

How can overcome that problem. Thanks in advance.

Member Avatar for iamthwee

Can you post the xml file...

My xml file is very long, so I copy the part of my xml file :

<?xml version="1.0"?>
<Root>
<User>
<name>Ronald</name>
<surname>Reagan</surname>
<TCIdno secrecy="c">33549566503</TCIdno>
<department>White house</department>
<rank>The president</rank>
<telNo>3354</telNo>
<deckNo>3354</deckNo>
<mobileNo secrecy="c">05323354956</mobileNo>
<homeTelNo secrecy="c">03123354956</homeTelNo>
</User>
<User>
<name>John Fitzgerald</name>
<surname>Kennedy</surname>
<TCIdno secrecy="c">33549566503</TCIdno>
<department>White house</department>
<rank>The president</rank>
<telNo>3354</telNo>
<deckNo>3354</deckNo>
<mobileNo secrecy="c">05323354956</mobileNo>
<homeTelNo secrecy="c">03123354956</homeTelNo>
</User>
<User>
<name>Hillary</name>
<surname>Clinton</surname>
<TCIdno secrecy="c">33549566503</TCIdno>
<department>White House</department>
<rank>Foreign Minister</rank>
<telNo>3354</telNo>
<deckNo>3354</deckNo>
<mobileNo secrecy="c">05323354956</mobileNo>
<homeTelNo secrecy="c">03123354956</homeTelNo>
</User>
</Root>
Member Avatar for iamthwee

Sorry, can you give a brief description of what you are trying to achieve.

I can't quite work it out.

My aim is to searching for a "User" in XML file base on his "name" ,"surname" or "department".
User enters a name in html form, and this php file process the request.
"areaPointer" holds which area is to be searched, and search is done according to that value.
In the "for" loop, I would like to traverse through the datas in the xml file. If any user's name is same with the name that user demands, it prints to output.
The code above is the part of the whole code.

I update the code. Here is the content. The problem that I encounter is in the comments:

$doc = new DOMDocument();
    $doc->load('http://localhost/xampp/tsbs_yellow_pages/run/1/databank.xml');
    $xPath = new DOMXPath($doc);
    //Declerations
    $phase;
    $length_of_phase;
    $nodeList;
	
    if($in_name != '')
    {
         //User want to search "name" tag.
 	$nodeList = $doc->getElementsByTagName('name');
	$phase = trim(($in_name));
	$length_of_phase = strlen($phase);
				
    }else if($in_surname != '')
    {
         //User want to search "surname" tag.
         $nodeList = $doc->getElementsByTagName('surname');
	$phase = trim($in_surname);
	$length_of_phase = strlen($phase);
		
    }else if($in_department != '')
    {
         //User want to search "department" tag.
	$nodeList = $doc->getElementsByTagName('department');
	$phase = trim($in_department);
	$length_of_phase = strlen($phase);
    }

    //Traversing datas in the xml file in order to find pairing "User".
    for($i=0; $i<900 ;$i++)
	{ 
		
		if(  strncasecmp( $nodeList->item($i)->nodeValue,$phase,$length_of_phase) == 0)
		{			
		         //demanded value is found.$nodeList->item($i)holds the "name"
                            //I have get the father of "name" ,say "User"
			$fatherNode = $nodeList->item($i)->parentNode;
			$childrenList = $fatherNode->childNodes;
			
			foreach ($childrenList as $node) {
                                     //Problem is here. How can I get surname, TCIdno and the other tags? 				echo "name: <"; //#
				echo $node->nodeValue;  //#
				echo "><p></p>"; //#
			}	
		
		}
		
	}

Assume that user tried to search for person whose name is "Ronald".The code finds the User with name is "Ronald", but I want to print the surname,department, rank tags belongs to "Ronald" user. I do not want to print tags with secrecy attribute is "c". These tags are confidential. This is the point and any help is greatly appreciated.

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.