Solved

Dynamic TreeView

Posted on 2006-06-20
5
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Title # Comments Views Activity
clicking to find my folders on wamp 8 47
Delete image(s) associated with record(s) 16 46
Which is best Image Resizing Web service 11 42
Phone number mask 4 16
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

710 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