?
Solved

Improving a TreeView Script

Posted on 2006-06-22
10
Medium Priority
?
202 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
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!

 
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 2000 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

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 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