SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to select a hierarchical post list

    (I originally put this post in the PHP forum. But here in databases is a better fit. Perhaps an admin will delete the PHP post.)

    I want a (php-based) forum I can install as the contents of a DIV, rather than as a independent page (no HTML, HEAD or BODY elements). There are a few simple forums out there that could be hacked into submission, but none that support a "hierarchical, threaded message" view, which I also want.

    So I'm thinking about writing my own bare bones but threaded forum (similar to the way usenet displays).
    Perhaps this could be done with three tables: user, forum and post,
    where the post.parent_post_id would be zero for thread heads.
    post.indent_count would be one more than the indent_count for the parent_post_id.
    The initial forum display would show thread head subjects only, where post.post_parent_id = 0.
    But if any such thread head link was selected an indented/threaded list of child posts would be be displayed too.

    Ordering the display, so all the first level children of post_id = X would appear both indented and below the parent subject line could be ddone with an enormously expensive recursive procedure. But I doubt that's a good idea.

    Perhaps there is a way to use a table similar to what is below, combined with "order by" and "group by" ....perhaps using a cursor or two, to pull out the posts in the requisite order, and where post.indent_count kludges the hierarchical display
    ...or am I barking up an impossible tree?

    CREATE TABLE post (
    post_id int(4) NOT NULL auto_increment,
    forum_id int(4) NOT NULL references forum(fid),
    user_id int(4) NOT NULL references thread_user(uid),
    parent_post_id int(4) NOT NULL default 0 references post(post_id),
    indent_count int(4) NOT NULL default 0,
    title varchar(96) NOT NULL,
    body text,
    PRIMARY KEY (post_id)
    ) ENGINE=MyISAM

    ...what would the select be?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    was there a database question in there somewhere?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What would be the select statement?

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    the select statement for what?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well. I doubt it's possible. But it would be powerful if it was...

    To retrieve posts (from the example table at thread top) in a complex ordering, where each post would be immediately followed by its first level child responses, and (recursively) where each first level child post would be followed by a list of its first level child responses. This could be done with a separate recursive select for each post in a hierarchy. But that would be too expensive in a busy forum.

    Could this be the output of a single select?
    If the ordering were correct, and because each such post would have a post.indent_count, a usenet-like hierarchical display could be made.......hopefully form a single select statement.

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    if you want to eliminate looping or recursion, you could use celko's nested set model

    with your adjacency model (characterized by the parent_id foreign key), you can also set up a single query with as many joins as there are levels in the hierarchy

    see Categories and Subcategories
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. That helps.
    (Celko etc...I do have his Trees and Hierarchies book)

    I did look at the source code for the phorum forum (which does support threaded displays).
    They use a relatively simple select to populate a hashed hash. And then use post processing sort routines (in PHP) to rearrange that list, prior to constructing the visual display.

    Threaded forums are tricky. That's probably why so few of them support it.

    ======another tack========
    Posts could be separated out into two tables: thread_head and thread_response.
    The thread_head table could hold an XML blob that would get updated for each response added to that thread. The XML would maintain the parent child relationships (sequence and indenting data). Mysql now supports XPATH queries. So the sequencing and indenting of the hierarchical display (and link construction) could come from the XML. Clicking any such individual link would invoke a subsequent sql select to get author, subject, post date and response text.

  8. #8
    Non-Member Max Height's Avatar
    Join Date
    Dec 2011
    Posts
    303
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Personally I would use the nested set model because I find it easier. But both the nested set and adjacency list models have their pros and cons.

    With the nested set model you can have unlimited categories and sub categories and you are simply using "left" and "right" values to denote the position of a node (category of some sort) in the hierarchy. To add/delete/move nodes (and any of their children) all you essentially do is manipulate the left and right values of the nodes.


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
  •