Solved

Need help with creating nested for XML

Posted on 2011-02-15
8
424 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/&#39;" 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 108

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
 

Author Comment

by:morako
ID: 34902185
instead of this..

node1
 - node3
  - node7
  - node8
 - node4
  - node9
  - node10
   - node11
   - node12
node2
 - node5
 - node6
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mySql Syntax 7 33
Problem with SqlConnection 5 123
Codiing Non-Existent Links 4 30
php connect() failed error 25 17
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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

747 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

14 Experts available now in Live!

Get 1:1 Help Now