Solved

Dynamic TreeView

Posted on 2006-06-20
5
326 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.

839 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