Page 1 of 1

displaying tree

Posted: Tue Jan 29, 2008 3:04 am
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>";
}
 

Re: displaying tree

Posted: Tue Jan 29, 2008 3:41 am
by Ollie Saunders
Please provide an example of what are you getting and what you would like to get?

Re: displaying tree

Posted: Tue Jan 29, 2008 3:41 am
by s.dot
Sounds to me like you need some recursion.

Re: displaying tree

Posted: Tue Jan 29, 2008 4:28 am
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> 

Re: displaying tree

Posted: Tue Jan 29, 2008 5:36 am
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.

Re: displaying tree

Posted: Tue Jan 29, 2008 6:40 am
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