Solved

Need help with creating nested for XML

Posted on 2011-02-15
8
432 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
  • 4
  • 3
8 Comments
 
LVL 109

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Creating and Managing Databases with phpMyAdmin in cPanel.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now