Solved

Need help with creating nested for XML

Posted on 2011-02-15
8
434 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
DevOps Toolchain Recommendations

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

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

829 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