Solved

Dynamic TreeView

Posted on 2006-06-20
5
323 Views
Last Modified: 2013-12-12
Hi guys,

I found a nice little DHTML Treeview:

http://www.dddekerf.dds.nl/DHTML_Treeview/DHTML_Treeview.htm

I now want to make it Generate on the fly from a MySQL Database Connection with the help of PHP. I've structured the Table the way I want it:

ID, Name, Parent
1, Analysis, 0
2, Implimentation, 0
3, PHP, 2
4, Visual C++, 2
5, Memory Leak problems, 4
6, Database problems, 4
7, Design, 0

The problem I am having is how do I make an infinite, yet escapable loop so I can loop through the database contents until I get the structure I want, or does somebody know an easier way of getting a nice simple treeview?

Cheers guys!
0
Comment
Question by:Cyber-Drugs
  • 2
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
Khanh Doan earned 500 total points
ID: 16941000
Oh, this is like my code.
I will post it for you. Or you can come to http://landautien.com/forum/music.php
I use it at Categories
My code :

/*********** PHP ************/
<?php
//########################### Cat List ###########################//
function catlist()
{
     $cat = array();
     $totalcats = '1';
     $maxid = '1';

     $cat_query = mysql_query("SELECT * FROM songcat ORDER BY displayorder, cat_name ASC");

     while ($cattemp = mysql_fetch_array($cat_query))
     {
          $id = $cattemp['catid'];
          if ($maxid < $id) $maxid = $id;

          $cat[$totalcats]['id'] = $totalcats;
          $cat[$totalcats]['cat_id'] = $cattemp['catid'];
          $cat[$totalcats]['parent_id'] = $cattemp['parent_catid'];
          $cat[$totalcats]['name'] = $cattemp['cat_name'];

          $totalcats++;
     }

     if ($totalcats == '1')
     {
          $cat_list_bit = $vbphrase['music_nocat'];
     }
     else
     {
          for ($cid = '1'; $cid <= $maxid; $cid++)
          {
               if ($cat[$cid]['parent_id'] == '0')
               {
                    $cat_id = $cat[$cid]['cat_id'];
                    $cat_name = $cat[$cid]['name'];
                    $par_num = '0';
                    $cat_list_sub = subcat($cat_id, $cat, $maxid);

                    $cat_list_bit .= "     <img src='images/misc/navbits_start.gif' border='0' class='inlineimg' style="cursor:hand;" onClick=\"hide_sub_cat(c$cat_id)\">
     <a href=' '><b>$cat_name</b></a><br>
     <span id=\"c$cat_id\" style=\"display : 'none'\"><div style=\"margin-left: 15px\">
               $cat_list_sub
     </div></span>";
               }
          }
     }

     $cat_list = $cat_list_bit;

     return $cat_list;
}

//########################### Sub Categories ###########################//
function subcat($parent_id, $cat, $maxid)
{
     global $vbulletin, $vbphrase;

     for ($sub_id = 0; $sub_id <= $maxid; $sub_id++)
     {
          if ($cat[$sub_id]['parent_id'] == $parent_id)
          {
               $cat_id = $cat[$sub_id]['cat_id'];
               $cat_name = $cat[$sub_id]['name'];

               for ($n = '1'; $n <= $par_num; $n++) $sub_cats_ .= '<img src="clear.gif" width="15" border="0" class="inlineimg">';

               $sub_cats_sub = subcat($cat_id, $cat, $maxid);

               $sub_cats .= "     $sub_cats_ <img src='images/misc/navbits_start.gif' border='0' class='inlineimg' style=\"cursor:hand\" onClick=\"hide_sub_cat(c$cat_id)\"> <a href=' '>$cat_name</a><br>
     <span id=\"c$cat_id\" style=\"display : 'none'\"><div style=\"margin-left: 15px\">
          $sub_cats_sub
     </div></span>";
          }
     }

     return $sub_cats;
}
?>

/*********** Database ************/
CREATE TABLE IF NOT EXISTS songcat (
  catid int(11) NOT NULL auto_increment,
  parent_catid int(11) NOT NULL default '0',
  cat_name varchar(250) NOT NULL default '',
  displayorder INT NOT NULL DEFAULT '0';
  PRIMARY KEY  (catid)
) TYPE=MyISAM COMMENT='Music Category';


/*********** JScript ************/
// ######################################################### //
// Show/Hide Sub categories
function hide_sub_cat(header)
{
      var head = header.style;
      if (head.display == 'none')
      {
            head.display = '';
      }
      else
      {
            head.display = 'none';
      }
}

So, just call catlist() function to get categories list.
Goodluck.
Bonmat86
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 16941657
Looks good, but when I run it, I get an error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/ourklub/public_html/CartoLogix/tmp/index.php on line 17
0
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 16942186
<?php
mysql_connect('localhost', 'root', 'root');
mysql_select_db('vbb');
//########################### Cat List ###########################//
function catlist()
{
     $cat = array();
     $totalcats = '1';
     $maxid = '1';

     $cat_query = mysql_query("SELECT * FROM songcat ORDER BY displayorder, cat_name ASC");

     while ($cattemp = mysql_fetch_array($cat_query))
     {
          $id = $cattemp['catid'];
          if ($maxid < $id) $maxid = $id;

          $cat[$totalcats]['id'] = $totalcats;
          $cat[$totalcats]['cat_id'] = $cattemp['catid'];
          $cat[$totalcats]['parent_id'] = $cattemp['parent_catid'];
          $cat[$totalcats]['name'] = $cattemp['cat_name'];

          $totalcats++;
     }

     if ($totalcats == '1')
     {
          $cat_list_bit = $vbphrase['music_nocat'];
     }
     else
     {
          for ($cid = '1'; $cid <= $maxid; $cid++)
          {
               if ($cat[$cid]['parent_id'] == '0')
               {
                    $cat_id = $cat[$cid]['cat_id'];
                    $cat_name = $cat[$cid]['name'];
                    $par_num = '0';
                    $cat_list_sub = subcat($cat_id, $cat, $maxid);

                    $cat_list_bit .= "     <img src='images/misc/navbits_start.gif' border='0' class='inlineimg' style=\"cursor:hand;\" onClick=\"hide_sub_cat(c$cat_id)\">
     <a href=' '><b>$cat_name</b></a><br>
     <span id=\"c$cat_id\" style=\"display : 'none'\"><div style=\"margin-left: 15px\">
               $cat_list_sub
     </div></span>";
               }
          }
     }

     $cat_list = $cat_list_bit;

     return $cat_list;
}

//########################### Sub Categories ###########################//
function subcat($parent_id, $cat, $maxid)
{
     global $vbulletin, $vbphrase;

     for ($sub_id = 0; $sub_id <= $maxid; $sub_id++)
     {
          if ($cat[$sub_id]['parent_id'] == $parent_id)
          {
               $cat_id = $cat[$sub_id]['cat_id'];
               $cat_name = $cat[$sub_id]['name'];

               for ($n = '1'; $n <= $par_num; $n++) $sub_cats_ .= '<img src="clear.gif" width="15" border="0" class="inlineimg">';

               $sub_cats_sub = subcat($cat_id, $cat, $maxid);

               $sub_cats .= "     $sub_cats_ <img src='images/misc/navbits_start.gif' border='0' class='inlineimg' style=\"cursor:hand\" onClick=\"hide_sub_cat(c$cat_id)\"> <a href=' '>$cat_name</a><br>
     <span id=\"c$cat_id\" style=\"display : 'none'\"><div style=\"margin-left: 15px\">
          $sub_cats_sub
     </div></span>";
          }
     }

     return $sub_cats;
}
echo catlist();
?>

I tested it and it works.
Bonmat86.
0
 
LVL 20

Expert Comment

by:Muhammad Wasif
ID: 16943965
You can try this one too
http://www.destroydrop.com/javascripts/tree/

This is very easy to implement with MySQL and PHP
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 16944720
I found the problem, there was a typographical error on my part, works a charm, 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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to count occurrences of each item in an array.

760 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

16 Experts available now in Live!

Get 1:1 Help Now