Solved

Dynamic TreeView

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
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.

749 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