generate hierarchical tree view

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
User avatar
dheeraj
Forum Commoner
Posts: 40
Joined: Fri Feb 06, 2009 11:54 am

generate hierarchical tree view

Post by dheeraj »

hello guys, i m trying to generate hierarchical tree view, as per done in any MLM business, where binary level or unary level tree has made....

in my problem there is a super user, below him there are many sub users this level will be called 1st level.
after tht the user's of first level will contain another child user, tht will be 2nd level, after 2nd there is 3rd level, then 4th & so on.... upto 8th level....

so i want to show this in tree view, it could be horizontal or vertical.... bt i m nt getting hw to do it... so plz help me...

Thanks.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: generate hierarchical tree view

Post by requinix »

So the first level can have any number of users but after that only one per parent? Odd.

You can take the same approach to this as you would if it were a normal (any number of children per parent) tree.

This is the easiest form of HTML tree to build:
  • Root
    • A
      • 1
        • i
    • B
      • 2
    • C
      • 3
        • iii
    • D
What does your database look like?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: generate hierarchical tree view

Post by aceconcepts »

User avatar
dheeraj
Forum Commoner
Posts: 40
Joined: Fri Feb 06, 2009 11:54 am

Re: generate hierarchical tree view

Post by dheeraj »

Thanx tasairis to reply....

i want the same thing u hd drawan there....
& i m taking a numeric user_ids for all the user, & a username & both will be unique...
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: generate hierarchical tree view

Post by requinix »

The HTML for such a structure looks like

Code: Select all

<ul>
    <li>Item<ul>
        <li>Sub item<ul>
            <li>Sub sub item</li></ul></li>
        <li>Next sub item</li></ul></li>
    <li>Next item<ul>
        <li>Sub next item</li></ul></li>
</ul>
<ul> starts a list, <li></li> marks an item, and </ul> ends the list.

Now,

Code: Select all

function makeListForUser($user) {
    echo "<ul>";
    /* foreach list of sub users for $user as $subuser */ {
        echo "<li>"; /* echo $subuser; */
        makeListForUser(/* $subuser */);
        echo "</li>";
    }
    echo "</ul>";
}
makeListForUser(/* root user */);
Try to fill in the comments there.
Post Reply