displaying tree

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

displaying tree

Post by thatsme »

I dowloaded a script from, http://www.mattkruse.com/javascript/mktree/index.html to display tree view.

I am only getting Parent array listed.
If i expand the parent array, all the child array element are listed. I should get another expandable parent array untill it does not have any child under it.

Code: Select all

 
//
...
$array_parents = array(1,2,3,4,5,6);
$array_childs = array('1.A', '1.A.1', '1.A.1.a', '1.A.1.a.1', '1.A.1.a.2', '1.A.1.a.3', '1.A.1.a.4', '2.A.1.3');
 
foreach($array_parents as $array_parent)
{
  echo "<ul class='mktree'>";
  echo "<li> $array_parent </li>";
 
  foreach($array_childs as $array_child)
  {
      if($array_parent == $array_child[0])
      {
        echo "<ul>";
        echo "<li> $array_child </li>";
        echo "</ul>";
      }
  }
 
  echo "</ul>";
}
 
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: displaying tree

Post by Ollie Saunders »

Please provide an example of what are you getting and what you would like to get?
Last edited by Ollie Saunders on Tue Jan 29, 2008 3:54 am, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: displaying tree

Post by s.dot »

Sounds to me like you need some recursion.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

Re: displaying tree

Post by thatsme »

i want to display tree view of the data (parent_array, child array).

sample data:

$array_parents = array(1,2,3,4,5,6);
$array_childs = array('1', '1.A', '1.A.1', '1.A.1.a', '1.A.1.a.1', '1.A.1.a.2', '1.A.1.a.3', '1.A.1.a.4', '2.A.1.3');

'1', '1.A', '1.A.1', '1.A.1.a', '1.A.1.a.1', '1.A.1.a.2', '1.A.1.a.3', '1.A.1.a.4' are children of parent 1(from parent array)

'2.A.1.3' is the child of 2 (from parent array)

To make tree, i downloaded the script from http://www.mattkruse.com/javascript/mktree/index.html. class="mktree" is the class which is given in that file.

//html code. This is what i am looking for. written manually. It should be dynamically generated..

Code: Select all

 <ul class="mktree"><li> 1  <ul>    <li>1.A      <ul>        <li>          1.A.1          <ul>            <li>1.A.1.a              <ul>                <li>                 1.A.1.a.1                </li>                <li>     1.A.1.a.2                </li>              </ul>            </li>           </ul>        </li>      </ul>    </li>  </ul> 
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: displaying tree

Post by Ollie Saunders »

Well now, the code you've currently got won't do that. So, extrapolating your reply above, I guess you want to do complex numbering for nested lists akin to this:

Code: Select all

1. bird
2. lizard
3. mammal
  3.a. whale
  3.b. cat
   3.b.i. tiger
   3.b.ii. lion
4. insect
  4.a. flea
I would suggest you either
  1. find another script that will actually do this
  2. write it yourself
  3. get someone to write it for you
Remember, we do not write code for people here only help those trying to do it for themselves. So if you're interested in option 2 I'll give you some pointers on how to get started.
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

Re: displaying tree

Post by thatsme »

This is the code i wrote,

Code: Select all

 
 
...
$array_parents = array(1,2,3,4,5,6);
$array_childs = array('1.A', '1.A.1', '1.A.1.a', '1.A.1.a.1', '1.A.1.a.2', '1.A.1.a.3', '1.A.1.a.4', '2.A.1.3');
 
foreach($array_parents as $array_parent)
{
  echo "<ul class='mktree'>";
  echo "<li> $array_parent </li>";
 
  foreach($array_childs as $array_child)
  {
      if($array_parent == $array_child[0])
      {
        echo "<ul>";
        echo "<li> $array_child </li>";
        echo "</ul>";
      }
  }
 
  echo "</ul>";
}
 
The parent elements are displaying along with the expandable and collapsable image. The immediate child is not displaying e&c image.

modified the code to

Code: Select all

 
 
for($i=0; $i<=9; $i++)
        {
          if(strlen($array_child) == $i)
          {
            if(strlen($array_child) == 9) // this 9 is statically given counting the number of maximum characters for parent 1, as it is for testing.
              echo "<li>$array_child</li>";
            else
              echo "<ul><li>$array_child";
 
          }
 
 
 
the last child the immediate above 1.e, 1.A.1.a is not showing the e&c image. Another thing is, its not showing the other parents. i.e, 2,3,4,5,6
Post Reply