Recursion is the way I manage this in my forum script.  The database
table for a particular forum topic does not have any concept of "threads"
in the field data.  Each message only knows who its parent is.  If the
parent ID = 0, then I know this is the first message of a virtual thread.

Have a look at an example...

I use a recursive loop to find children of children.  In the main screen I
use it to build indented lists of the first 30 "threads."  Within a
message, I use it to find the children of children of the current message

So long as you have proper indexes in your table for the main ID and the
ParentID, this will be blazing fast. 

I have thought long and hard about seperating the thread and post data
into two tables.  I see some advantages to this, but so far, I just cannot
justify the need for it when the thread relationship is implicit within
the defined parent child relationship of each message.

The result is a simple one-table-per-topic system with the content of the
table simply being:

- ID = autoincrement
- Title = The message Title
- Author = Who made this message - derived from a seperate login system
- Date = The data message was created - simply NOW() in the insert query
- ParentID = The ID of the message that is parent to this, 0 if thread top
- Body = The actual message
- ClientIPAddress = I log this, but probably don't really need to.
- Display = An on-off switch to hide inappropriate messages.
- hash = I calculate an MD5 hash of Title-Author-Body and UNIQUE it to
prevent duplicate postings.

Are their better ways?  Sure.  Do I care?  Not yet as this just works fine
for me.

Good luck.


On Wed, 24 Apr 2002, Phil Schwarzmann wrote:

>-I want to write a simple bulletin board in PHP.  I want to use the
>-typical parent-child (it's sorted by both date AND thread) type of
>-bulletin board you see often on other websites.
>-But I just can't figure out how to write some simple code to do this
>-and keep track of all the threads.
>-Any ideas?  I don't even need example code, just if someone could
>-quickly explain in plain english how to do it.


John Huggins



PHP General Mailing List (
To unsubscribe, visit:

Reply via email to