?
Solved

Need help with creating nested for XML

Posted on 2011-02-15
8
Medium Priority
?
442 Views
Last Modified: 2012-05-11
Hello EE,

Need help with creating nested for XML. and the working code is displayed here: http://wwbn.co/bans/tree.php.

currently the code uses " " to indicate nesting, but I need to convert this to look like this code below in order to use a JQuery tree.

<menuTree>
    <link linkName="300-Level"  type="parent" parentId="300-Level"  >
        <link linkName="CSCI340: ype="parent" parentId="CSCI340">
            <link linkName="Contact Us" linkOnClick="window.location=&#39; http://royjin.wufoo.com/forms/z7x4a9/';" type="child" childIcon="contactUs"></link>
            <link linkName="Discussion" type="child" childIcon="discussion"></link>
            <link linkName="Lectures" type="parent" parentId="CSCI102Lectures" parentIcon="lectureIcon">
                <link linkName="Lecture 1: Introduction to operating systems" type="child"></link>
            </link>          
            <link linkName="Tests" type="parent" parentId="CSCI340Tests" parentIcon="testsIcon">
                <link linkName="Test 1" type="child" linkTarget="http://roy-jin.appspot.com/jsp/jqueryXmlMenuTreeDemo.jsp"></link>
                <link linkName="Test 2" type="child" linkTarget="http://roy-jin.appspot.com/jsp/jqueryXmlMenuTreeDemo.jsp" ></link>
            </link>
            <link linkName="Winning"  type="child" childIcon="winning"></link>
        </link>
    </link>
</menuTree>

Any ideas..

Thanks in advance.
if ( $child['category_id'] != $child['parent_id'] )
		{
			for ( $c=0;$c<$depth;$c++ )			// Indent over so that there is distinction between levels
			{ $tempTree .= "&nbsp;"; }
			$tempTree .= "- " . $child['title'] . "<br>";
			$depth++;		// Incriment depth b/c we're building this child's child tree  (complicated yet???)
			$tempTree .= build_child($child['category_id']);		// Add to the temporary local tree
			$depth--;		// Decrement depth b/c we're done building the child's child tree.
			array_push($exclude, $child['category_id']);			// Add the item to the exclusion list
		}

Open in new window

<?php
 
// Recursive function to generate a parent/child tree
// Without the need for a Root parent
// Written by: Brian Parnes
// 13 March 2006
 
//$connect = mysql_connect(HOST_NAME, USERNAME, PASSWORD);
//mysql_select_db(DATABASE);
$nav_query = mysql_query("SELECT * FROM `categories` ORDER BY `category_id`");
$tree = "";					// Clear the directory tree
$depth = 1;					// Child level depth.
$top_level_on = 1;			// What top-level category are we on?
$exclude = array();			// Define the exclusion array
array_push($exclude, 0);	// Put a starting value in it
 
while ( $nav_row = mysql_fetch_array($nav_query) )
{
	$goOn = 1;			// Resets variable to allow us to continue building out the tree.
	for($x = 0; $x < count($exclude); $x++ )		// Check to see if the new item has been used
	{
		if ( $exclude[$x] == $nav_row['category_id'] )
		{
			$goOn = 0;
			break;				// Stop looking b/c we already found that it's in the exclusion list and we can't continue to process this node
		}
	}
	if ( $goOn == 1 )
	{
		$tree .= $nav_row['title'] . "<br>";				// Process the main tree node
		array_push($exclude, $nav_row['category_id']);		// Add to the exclusion list
		if ( $nav_row['category_id'] < 6 )
		{ $top_level_on = $nav_row['category_id']; }
 
		$tree .= build_child($nav_row['category_id']);		// Start the recursive function of building the child tree
	}
}
 
function build_child($oldID)			// Recursive function to get all of the children...unlimited depth
{
	global $exclude, $depth;			// Refer to the global array defined at the top of this script
	$child_query = mysql_query("SELECT * FROM `categories` WHERE parent_id=" . $oldID);
	while ( $child = mysql_fetch_array($child_query) )
	{
		if ( $child['category_id'] != $child['parent_id'] )
		{
			for ( $c=0;$c<$depth;$c++ )			// Indent over so that there is distinction between levels
			{ $tempTree .= "&nbsp;"; }
			$tempTree .= "- " . $child['title'] . "<br>";
			$depth++;		// Incriment depth b/c we're building this child's child tree  (complicated yet???)
			$tempTree .= build_child($child['category_id']);		// Add to the temporary local tree
			$depth--;		// Decrement depth b/c we're done building the child's child tree.
			array_push($exclude, $child['category_id']);			// Add the item to the exclusion list
		}
	}
 
	return $tempTree;		// Return the entire child tree
}
 
echo $tree;
 
?>

Open in new window

0
Comment
Question by:morako
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34901784
"the working code is displayed here: http://wwbn.co/bans/tree.php"

Authorization Required

This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.
Apache/2.2.3 (CentOS) Server at wwbn.co Port 80
0
 

Author Comment

by:morako
ID: 34901891
Try it now.  ;-)
0
 

Author Comment

by:morako
ID: 34902075
I would like it to do this:

<menuTree>
          <link linkName="node1">
              <link linkName="node3">
                        <link linkName="node7"></link>
                        <link linkName="node8"></link>
            </link>
              <link linkName="node4">
                  <link linkName="node9"></link>
                  <link linkName="node10">
                        <link linkName="node11"></link>
                        <link linkName="node12"></link>
                  </link>
                </link>
      </link>
      <link linkName="node2">
                  <link linkName="node5"></link>
                  <link linkName="node6"></link>
      </link>
      
</menuTree>
<menuTree>
    	<link linkName="node1">
        	<link linkName="node3">
            		<link linkName="node7"></link>
            		<link linkName="node8"></link>
		</link>
        	<link linkName="node4">
			<link linkName="node9"></link>
			<link linkName="node10">
				<link linkName="node11"></link>
				<link linkName="node12"></link>
			</link>
    		</link>
	</link>
	<link linkName="node2">
			<link linkName="node5"></link>
			<link linkName="node6"></link>
	</link>
	
</menuTree>

Open in new window

0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

Author Comment

by:morako
ID: 34902185
instead of this..

node1
 - node3
  - node7
  - node8
 - node4
  - node9
  - node10
   - node11
   - node12
node2
 - node5
 - node6
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34904073
try replacing this
$tempTree .= "- " . $child['title'] . "<br>";
into
$tempTree .= "&lt; link linkName=\"" . $child['title'] . "\"&gt;&lt;/link&gt;<br>";
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 2000 total points
ID: 34904080
once again with tab
$tempTree .= "\t&lt; link linkName=\"" . $child['title'] . "\"&gt;&lt;/link&gt;<br>";

Open in new window

0
 

Author Closing Comment

by:morako
ID: 34906534
Thanks pal.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34906547
Cheers.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question