Link to home
Start Free TrialLog in
Avatar of Cyber-Drugs
Cyber-DrugsFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Dynamic TreeView

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!
ASKER CERTIFIED SOLUTION
Avatar of Khanh Doan
Khanh Doan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cyber-Drugs

ASKER

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
<?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.
You can try this one too
http://www.destroydrop.com/javascripts/tree/

This is very easy to implement with MySQL and PHP
I found the problem, there was a typographical error on my part, works a charm, cheers!! :)