# Final question xml php almost there check it

**URL:** <https://forum.kirupa.com/t/final-question-xml-php-almost-there-check-it/253392>\
**Category:** programming\
**Created:** [February 29, 2008, 6:12pm UTC](https://forum.kirupa.com/t/final-question-xml-php-almost-there-check-it/253392 "2008-02-29T18:12:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Multi\_Task](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@Multi\_Task](https://forum.kirupa.com/u/Multi_Task)\
**Post date:** [February 29, 2008, 6:12pm UTC](https://forum.kirupa.com/t/final-question-xml-php-almost-there-check-it/253392/1 "2008-02-29T18:12:42Z")

</div>

okay I am trying to create a menu via xml which xml is generated via php and mysql.  
I am trying to link all pages under their corresponding subjects here is how I am going about it.

```auto

function flash_navigation() {
		global $connection;
		$query = "SELECT a.menu_name, b.subject_name FROM ";
		$query .= " pages a ";
		$query .= " inner join subjects b ";
		$query .= " on b.id = a.subject_id ";
		$result = mysql_query($query, $connection) or die("Could Not Get");
		$num = mysql_num_rows($result);
		if ($num != 0) {
		$file= fopen("results.xml", "w");

 		$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
";

 		$_xml .="<menu>
";

		while ($row = mysql_fetch_array($result)) {

 		if ($row['subject_name']) {

                $_xml .="	<item name=\"" . $row['subject_name'] . "\">
";

                $_xml .=" <sub name=\"" . $row['menu_name'] . "\"/>
";
                $_xml .="	</item>
";
     } else {

 $_xml .="	<item name=\"Nothing Returned\">
";
$_xml .=" <sub name =\"Nothing Returned\">
";

 $_xml .="	</item_name>
";
 } 
}

 $_xml .="</menu>";

 fwrite($file, $_xml);

 fclose($file);

 echo "XML has been written. <a href=\"results.xml\">View the XML.</a>";

 } else {

 echo "No Records found";

 }
}

```

This is returning somewhat usable xml but it is not grouping all pages with subjects instead creates a new item name tag if there is another page that corresponds with a subject. Like below. Also if a project is in database but has no pages correlating it doesnt return the project.

```auto

<?xml version="1.0" encoding="UTF-8" ?>
<menu>
	<item name="Residential 1">
		<sub name="dude"/>
	</item>
	<item name="Residential 1">
		<sub name="woompa"/>
	</item>
	<item name="Residential 2">
		<sub name="masa"/>
	</item>
	<item name="Commercial 1">
		<sub name="gatorade"/>
	</item>
	<item name="Commercial 2">
		<sub name="okao"/>
	</item>
</menu>

```

How can I group them where it would be like:

```auto

<?xml version="1.0" encoding="UTF-8" ?>
<menu>
	<item name="Residential 1">
		<sub name="dude"/>
                <sub name="woompa"/>
	</item>
	<item name="Residential 2">
		<sub name="masa"/>
	</item>
	<item name="Commercial 1">
		<sub name="gatorade"/>
	</item>
	<item name="Commercial 2">
		<sub name="okao"/>
	</item>
	<item name="Residential 1"> <<<<< Here it would show even if no pages	
	</item>
</menu>

```

I have tried setting another if statement correlating with proper $row and only if menu\_name was present but it does not function. Any ideas on how to create this?

MT
