Solved

Need help with creating nested for XML

Posted on 2011-02-15
8
437 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 110

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This article discusses how to implement server side field validation and display customized error messages to the client.
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.

717 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