Results 1 to 4 of 4

Thread: Tree Diagram/Map of posts/database entries?

  1. #1

    Thread Starter
    Lively Member Zolomon's Avatar
    Join Date
    Oct 2007
    Location
    Sweden
    Posts
    104

    Question Tree Diagram/Map of posts/database entries?

    Hello!

    I am trying to create a map, or a tree diagram if you will, of all the posts stored in a database, like so: "http://www.zolomon.eu/misc/treediagram.jpg". Could anyone provide me with tips of how I would be able to achieve something similar to this, or if there already are premade solutions like Flash diagrams?

    I have:
    post_id = ID for each post
    link_id = ID for the post which the current post links to, (ex: link_id = 1, then if post_ID = 2 this means that the current post, which is 2, links to the first post, which is 1)

    Any help at all is appreciated,
    Cheers!

  2. #2
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Tree Diagram/Map of posts/database entries?

    try this query:

    SELECT * FROM `mytable`
    My usual boring signature: Something

  3. #3

    Thread Starter
    Lively Member Zolomon's Avatar
    Join Date
    Oct 2007
    Location
    Sweden
    Posts
    104

    Re: Tree Diagram/Map of posts/database entries?

    If you could explain a bit further with what you are trying to get at, I shall try not to be rude in my next reply. (: joke)

    That would select everything from the table, and I most certainly do not want that, as stated in my question.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Tree Diagram/Map of posts/database entries?

    Your schema'd be a lot easier to understand if you used 'parent_id', which is unambiguous, instead of 'link_id', which is totally ambiguous.

    Anyway, your problem is of two parts:
    • parsing the database records into an in-memory tree structure;
    • presenting the tree graphically.


    The first part is easy. The second could be quite complex.

    dclamp is actually correct — you do need to select everything from the table in one go. Databases don't lend themselves well to multi-dimensional structures.
    Use a class to represent a tree node. It can be either singly-linked or double-linked, depending on whether you feel you need to walk both up and down the tree or not.
    PHP Code:
    class Node
    {
      public 
    $post_id;
      public 
    $parent;
      public 
    $children = array();

    I would also create a hashtable of nodes stored by post ID.
    Enumerate the database records and insert each record into the hashtable. You can look up the parent object in the hashtable and use that to populate the $parent and $children fields of the new node object.


    I trust that will get you started?

Posting Permissions

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



Click Here to Expand Forum to Full Width