SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating a flexible hierarchy

    How do I create a table structure and query that allows for a flexible hierarchy of records. For example, in a forum type of environment, I have a table that is

    t1.id, t1.message

    Then a relate table that is a parent/child as
    parentid, childid which are both t1.id numbers

    So if the table were populated with
    Code:
    1  2
    1  3
    2  4
    2  5
    4  6
    6  7
    8  0 (0 being no children)
    9  0
    And I want to display
    Code:
    1
       2
          4
             6
                7
          5
       3
    8
    9
    So is this the correct table structure and if I do it like that, is there a way to do the query to return all the records in this hierarchy order?

    Thanks

  2. #2
    SitePoint Evangelist speda1's Avatar
    Join Date
    Jan 2002
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Create a belongs_to field that contains the id of the parent.

  3. #3
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    471
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Makes sense, that saves a join on any query. Is there a way to build the hierarchy with a single query or does that need to be an iterative process from php?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •