Results 1 to 8 of 8

Thread: Table structure for forums.

  1. #1

    Thread Starter
    Addicted Member samkud's Avatar
    Join Date
    Oct 2001
    Location
    India
    Posts
    171

    Table structure for forums.

    Hi Folks,

    I am developing forums for a site. I just want to know what should be the table structure for storing the messages which are hierarchical i.e reply to a reply, reply to the second reply and so on.

    Also how to display each message under the parent message.

    The table structure I think of is

    Replyid|ForumID|TiTle|Message|MessageIcon|ParentReplyID|ReplyDate|ReplyModificationDate|

    ParentReplyID is the Replyid under which a particular message will come.

    But the problem is how to display them under the proper parent.

    Please enlighten me.

    Regards,

    Samir

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    Wrong forum buddy, try the HTML forum, PHP forum, etc...



    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3
    Fanatic Member MikkyThomeon's Avatar
    Join Date
    Oct 2002
    Location
    At work...
    Posts
    648
    You can create a single messages table and use foreign keys in the table to reference the primary key of the same table. That way you will be able to create recursive one to many relationships and display the whole realtionship tree to as may levels as you like. I created a sample project a while back but it uses VB, so its not your kitty here sadly.



    I would have the following

    Posts Table

    ipkPostID (Primary key - auto increemnt field)
    ifkPostID (references the primary key)
    vSubject
    vMessage
    dtPost

  4. #4

    Thread Starter
    Addicted Member samkud's Avatar
    Join Date
    Oct 2001
    Location
    India
    Posts
    171
    Thanx MikkyThomeon

    I got a fair idea about the table structure u are talking about. BUt how do I display them in a hierarchical form. Can u give a small example. Even if the code is in VB it will do. I just want the idea.

    Thanx in advance.

    Regards,

    Samir

  5. #5
    Fanatic Member MikkyThomeon's Avatar
    Join Date
    Oct 2002
    Location
    At work...
    Posts
    648
    The way I did it was using classes and collections, but seeing that you are not using VB then you will need to use a recursive function to show the hierarchy

    VB Code:
    1. Sub ShowPosts(cn As Connection, PostID As Long, NestLevel As Long)
    2.     Dim rs As Recordset
    3.     Dim NstLevel As Long
    4.     Dim Posts() As Long
    5.    
    6.     Set rs = cn.Execute("SELECT pkid,fkid,subject FROM posts WHERE ikfpostID = " & PostID)
    7.     'write html code here to show the subject, indented by the nest level
    8.     Do Until rs.EOF
    9.         NstLevel = NestLevel + 1
    10.         ShowPost cn, rs("fkid"), NstLevel
    11.         rs.MoveNext
    12.     Loop
    13. End Function

    something like this - have not tested it. The only limitation with this is the 32 bit stack.

  6. #6
    Fanatic Member MikkyThomeon's Avatar
    Join Date
    Oct 2002
    Location
    At work...
    Posts
    648
    oops

    VB Code:
    1. Set rs = cn.Execute("SELECT pkid,fkid,subject FROM posts WHERE fkid = " & PostID)

  7. #7

    Thread Starter
    Addicted Member samkud's Avatar
    Join Date
    Oct 2001
    Location
    India
    Posts
    171
    HI MikkyThomeon,

    Thanx alot. I Think this should help.

    BUt one doubt. See the subroutine u are using sets a recordset. SO each time this routine is called, the instance will be created. Is there a way by which can we set the recorset once and get the records using only one query. Do u think we should maintain levels in a separate column.

    Please let me know.

    Regards,

    Samir.

  8. #8
    Fanatic Member MikkyThomeon's Avatar
    Join Date
    Oct 2002
    Location
    At work...
    Posts
    648
    I agree with the single call to the database, so what you can do is pass a recordset as a parameter into the function.

    VB Code:
    1. Sub CallShowPosts()
    2.     Dim cn As Connection
    3.     Dim rs As Recordset
    4.     Set cn = New Connection
    5.     cn.CursorLocation = adUseClient
    6.     cn.Open "DSN=MyDSN"
    7.     Set rs = New Recordset
    8.     rs.Open "SELECT TOP 1000 * FROM Posts ORDER BY PostDate DESC", cn, adOpenStatic, adLockBatchOptimistic
    9.     ShowPosts rs, 10, 1
    10. End Sub
    11.  
    12.  
    13. Sub ShowPosts(ByVal rsPosts As Recordset, ByVal PostID As Long, NestLevel As Long)
    14.     Dim rs As Recordset
    15.     Dim NstLevel As Long
    16.    
    17.     rsPosts.Filter = ""
    18.     rsPosts.Filter = "fkid = " & PostID
    19.     'Set rs = cn.Execute("SELECT pkid,fkid,subject FROM posts WHERE ikfpostID = " & PostID)
    20.     'write html code here to show the subject, indented by the nest level
    21.     Do Until rs.EOF
    22.         NstLevel = NestLevel + 1
    23.         ShowPost rsPosts, rs("fkid"), NstLevel
    24.         rsPosts.MoveNext
    25.     Loop
    26. End Sub


    Here I have just massed the last 1000 records, so it is probably better that each thread in the folum is related by a single unique ID, so that when you pass the recordset into the Showposts function, you can issue a query like this:


    strSQL = "SELECT * FROM Posts WHERE TopicID = 23"

    This will then return a recordset that contains all the related posts on a subject, and you can pass this recordset into the Showposts function to sort it into a hierarchy

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