Solved

Improving a TreeView Script

Posted on 2006-06-22
10
192 Views
Last Modified: 2013-12-12
Hi guys,

I'm using this treeview script:

http://www.experts-exchange.com/Web/Web_Languages/PHP/PHP_Databases/Q_21892219.html

It works great, except I now want to alter it so that it displays roots on the view.

Example 1 (how it is at the moment)

A
  D
  E
    G
    H
  F
B
C


Example 2 (how I want it to look)

A
|-D
|-E
|-|-G
|-|-H
|-F
B
C



Not quite as tacky as that, but I'm sure that explains to you what I'm trying to do. Any ideas what needs to changing?

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

Expert Comment

by:snoyes_jw
Comment Utility
Replace the clear.gif with an image containing |-, or with the relevant text.
0
 
LVL 10

Expert Comment

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

Change it to

for ($n = '1'; $n <= $par_num; $n++) $sub_cats_ .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';

or

for ($n = '1'; $n <= $par_num; $n++) $sub_cats_ .= '| - ';

Bonmat86.
0
 
LVL 4

Author Comment

by:Cyber-Drugs
Comment Utility
Hi guys,

Sorry, but I forgot, I made a few changes to the code, so here's the updated code:

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='../ajax/images/minus.gif' border='0' style=\"cursor:hand;\" onClick=\"hide_sub_cat(c".$cat_id.", this)\">&nbsp;<a style=\"cursor:hand\" onclick=\"nav_click(".$cat_id.", '".$cat_name."')\"><strong>".$cat_name."</strong></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;
}

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_.='|-';
                  
                  $sub_cats_sub = subcat($cat_id, $cat, $maxid);
                  
                  $child_count = mysql_num_rows(mysql_query("SELECT * FROM songcat WHERE parent_catid='".$cat_id."'"));
                  if ($child_count == 0)
                  {
                        $sub_cats .= $sub_cats_." <img src='../ajax/images/leaf.gif' border='0' style=\"cursor:hand\" onClick=\"hide_sub_cat(c".$cat_id.", this)\">&nbsp;<a style=\"cursor:hand\" onclick=\"nav_click(".$cat_id.", '".$cat_name."')\">".$cat_name."<a/><br>
                        <span id=\"c".$cat_id."\" style=\"display:'none'\"><div style=\"margin-left: 15px\">".$sub_cats_sub."</div></span>";
                  }
                  else
                  {
                        $sub_cats .= $sub_cats_." <img src='../ajax/images/minus.gif' border='0' style=\"cursor:hand\" onClick=\"hide_sub_cat(c".$cat_id.", this)\">&nbsp;<a style=\"cursor:hand\" onclick=\"nav_click(".$cat_id.", '".$cat_name."')\">".$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;
}


As you may notice, I tried implimenting the '|-', but it doesn't display any different on my screen....
0
 
LVL 10

Expert Comment

by:Khanh Doan
Comment Utility
<?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 = '1';
                    $cat_list_sub = subcat($cat_id, $cat, $maxid, $par_num);

                    $cat_list_bit .= "
     <a href=' '><b>$cat_name</b></a><br>
     <span id=\"c$cat_id\" style=\"display : ''\">
               $cat_list_sub
     </span>";
               }
          }
     }

     $cat_list = $cat_list_bit;

     return $cat_list;
}

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

       $sub_cats_tree = '';

     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_tree .= '| - ';

               $sub_cats_sub = subcat($cat_id, $cat, $maxid, $par_num+1);

               $sub_cats .= $sub_cats_tree."<a href=' '>$cat_name</a><br>
     <span id=\"c$cat_id\" style=\"display : ''\">
          $sub_cats_sub
     </span>";
          }
     }

     return $sub_cats;
}

echo catlist();
?>

Bonmat86.
0
 
LVL 4

Author Comment

by:Cyber-Drugs
Comment Utility
Hi,

Thanks for the reply, but I don't believe you took into account the change that I made to the original code...


$child_count = mysql_num_rows(mysql_query("SELECT * FROM songcat WHERE parent_catid='".$cat_id."'"));
               if ($child_count == 0)
               {
                    $sub_cats .= $sub_cats_." <img src='../ajax/images/leaf.gif' border='0' style=\"cursor:hand\" onClick=\"hide_sub_cat(c".$cat_id.", this)\">&nbsp;<a style=\"cursor:hand\" onclick=\"nav_click(".$cat_id.", '".$cat_name."')\">".$cat_name."<a/><br>
                    <span id=\"c".$cat_id."\" style=\"display:'none'\"><div style=\"margin-left: 15px\">".$sub_cats_sub."</div></span>";
               }
               else
               {
                    $sub_cats .= $sub_cats_." <img src='../ajax/images/minus.gif' border='0' style=\"cursor:hand\" onClick=\"hide_sub_cat(c".$cat_id.", this)\">&nbsp;<a style=\"cursor:hand\" onclick=\"nav_click(".$cat_id.", '".$cat_name."')\">".$cat_name."</a><br>
                    <span id=\"c".$cat_id."\" style=\"display:'none'\"><div style=\"margin-left: 15px\">".$sub_cats_sub."</div></span>";
               }
          }
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 10

Expert Comment

by:Khanh Doan
Comment Utility
I think you will modify my code which i post above. Because i use <div> tag so you can list as :
A
  D
  E
    G
    H
  F
B
C

to list  as

A
|-D
|-E
|-|-G
|-|-H
|-F
B
C
you have to midify my code again. I add a $par_num argument in subcats function. so it work now.
Bonmat86.
0
 
LVL 4

Author Comment

by:Cyber-Drugs
Comment Utility
OK, I just tried the code and it doesn't give me what I'm after. I think it's because I may of explained it wrong...

Eg:


Here is what I want:

A
|-B
| |-C
| |-D
|-E
| |-F
| | |-G
| | | |-H
| | |-I
J

and this is what I'm getting:

A
|-B
|-|-C
|-|-D
|-E
|-|-F
|-|-|-G
|-|-|-|-H
|-|-|-I
J


I am after a clean and tidy treeview, much like the one you see on Windows Explorer.
0
 
LVL 4

Author Comment

by:Cyber-Drugs
Comment Utility
0
 
LVL 10

Accepted Solution

by:
Khanh Doan earned 500 total points
Comment Utility
function subcat($parent_id, $cat, $maxid, $par_num)
{
     global $vbulletin, $vbphrase;

      $sub_cats_tree = '';

     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_tree .= '| &nbsp;  ';

               $sub_cats_sub = subcat($cat_id, $cat, $maxid, $par_num+1);

               $sub_cats .= $sub_cats_tree."<a href=' '> - $cat_name</a><br>
     <span id=\"c$cat_id\" style=\"display : ''\">
          $sub_cats_sub
     </span>";
          }
     }

     return $sub_cats;
}

Try it please.
Bonmat86.
0
 
LVL 4

Author Comment

by:Cyber-Drugs
Comment Utility
That did the trick, I now just have to get some image files to replace the text and then I'm sorted, cheers!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

11 Experts available now in Live!

Get 1:1 Help Now