Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a php script ....


PHP
<?php
    $con = mysqli_connect("localhost", "root", "", "mchannel_learningtool");
	$sqlQury=mysqli_query($con,"SELECT Course_id,Course_name,course_type FROM course_master WHERE Is_active=1");
	$xml = new DOMDocument('1.0', 'UTF-8');
    $xml -> preserveWhiteSpace = FALSE;
    $courseList = $xml -> creatEelement("CourseList");
	$xml -> appendChild($courseList);
	while($row=mysqli_fetch_array($sqlQury))
	{
		
		$value=$row['Course_id'];
		$CoursesRoot=$xml->documentElement;
		$courses=$CoursesRoot->getElementsByTagName("Course");
			echo "<br>".$courses->length."Fetching from array ....<br>";
		if($courses->item(0))
		{
		
			$DesireCoursefound=false;	
		foreach($courses as $SingleCourse)
		{	echo "<br>if  item -1";
			if($SingleCourse->getElementsByTagName("Course_id")->item(0)->nodeValue==$value)
			{
				
				$DesireCoursefound=true;
			}
			if(!$DesireCoursefound)
			{
				//echo "<br>if  item -2";
				$Course=$xml->createElement("Course");
			$name=$xml->createElement("Course_name");
			$id=$xml->createElement("Course_id");
			$Type=$xml->createElement("Course_type");
			$id->nodeValue=$row['Course_id'];
			$name->nodeValue=$row['Course_name'];
			$Type->nodeValue=$row['course_type'];
			$Course->appendChild($id);
			$Course->appendChild($name);
			$Course->appendChild($Type);
			$courseList->appendChild($Course);
				
			}
		}
		}
		
		if(!$courses->item(0))
		{
			
			echo "<br>if no item";
			$Course=$xml->createElement("Course");
			$name=$xml->createElement("Course_name");
			$id=$xml->createElement("Course_id");
			$Type=$xml->createElement("Course_type");
			$id->nodeValue=$row['Course_id'];
			$name->nodeValue=$row['Course_name'];
			$Type->nodeValue=$row['course_type'];
			$Course->appendChild($id);
			$Course->appendChild($name);
			$Course->appendChild($Type);
			$courseList->appendChild($Course);
			
			
		}
		
		
		
	}

echo $xml->savexML();
$xml -> save("CourseList.xml");
	
	
?>



and i want to create a xml structure like ...


<courselist>
<course>
<course_id>1</course_id>
<course_name>physics</course_name>
<course_type>1</course_type>
</course>
<course>
<course_id>2</course_id>
<course_name>chemistry</course_name>
<course_type>2</course_type>
</course>
</courselist>


but in this xml some <course> node is repeating ..

i am unable to find out whats the problem ... ?
Posted
Updated 23-Dec-13 0:48am
v2

1 solution

a little mistake in this .. i have solve out this ... :)
 
Share this answer
 
Comments
Er. Tushar Srivastava 23-Dec-13 7:50am    
Please point out the mistake in the answer as it maybe helpful to someone who like you also get into similar problem :) If you just want to comment then please move it to Comment Section. This box is to submit answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900