Solved

Diplaying data in binary tree

Posted on 2006-07-09
7
376 Views
Last Modified: 2008-02-01
i have store some data in database( mysql). i want to display that data in a binary tree on web page. i m able to display some in binary tree but not all. is there any method to display that in binary form ?
i want to dispalay data in following manner
     a
  |    |
  b    c
      |   |
      d   e
 and so on.......
0
Comment
Question by:abhishek2002
  • 4
7 Comments
 
LVL 7

Accepted Solution

by:
maUru earned 250 total points
ID: 17072084
use a parent-child structure

ID, pID, data

so basically your tree would be saved as:

id , pid, data
1    0    a
2    1    b
3    1    c
4    3    d
5    3    e

pid is the id of the element directly above


and to display it, use a recursive function, there are many different algorithms, here is one i picked up from here:

(id = category_id, pid = parent_id, data = title)

so the table format for this code would be:

category_id, parent_id, title

<?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;

?>
0
 
LVL 7

Expert Comment

by:maUru
ID: 17072104
0
 

Author Comment

by:abhishek2002
ID: 17079012
i have implement this code. it is working .
thankyou
0
 
LVL 7

Expert Comment

by:maUru
ID: 17104967
np
0
 
LVL 7

Expert Comment

by:maUru
ID: 17123427
mind accepting the answer?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
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…
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.

948 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

20 Experts available now in Live!

Get 1:1 Help Now