Solved

Improving a TreeView Script

Posted on 2006-06-22
10
199 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
[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
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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.
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.

729 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