Solved

Improving a TreeView Script

Posted on 2006-06-22
10
194 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
ID: 16960610
Replace the clear.gif with an image containing |-, or with the relevant text.
0
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 16960966
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
ID: 16961170
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 10

Expert Comment

by:Khanh Doan
ID: 16961638
<?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
ID: 16963394
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
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 16965336
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
ID: 16966943
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
ID: 16966954
0
 
LVL 10

Accepted Solution

by:
Khanh Doan earned 500 total points
ID: 16966970
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
ID: 16967007
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

770 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