Page 1 of 1

Nested message board project, I need a hint or two

Posted: Wed Jan 14, 2009 1:27 am
by Yevgeni
I hope someone can give me some sort of help with this, as I've searched far and wide elsewhere on the internet.

I am programming a message board from the ground up due to the fact that there's nothing already out there that acts like I want it to act. You can check out my working version here to see where I'm going with it:
http://thelandofoz.pcriot.com/mb

As of now, this program's main use is on my website which is a fan site for a heavy metal band. The message board is definitely in a usable state as it sits. I plan on developing all sorts of features for this in the future, but right now, its layout is less than ideal. My original objective was to re-write a Perl message board called WWWBoard in PHP (you may be familiar with it). My program started out well, but I soon hit a brick wall. I just can't figure out the programming logic to make my message board behave like WWWBoard. Here's a demo WWWBoard:
http://www.scriptarchive.com/demos/wwwb ... board.html

Haha, this thing is spammed into oblivion. Anyway, by visiting my message board, you can see how it handles replies. It just sticks them on the page with the original thread. There is nothing wrong with this, but I want each message and each reply to have its own link like in WWWBoard, for example:

* Message1
* Re: Message1
* Re: Message1
*Re: Re: Message1
* Message2
* Re: Message2
* Re: Message2
* Re: Re: Message2
* Re: Re: Re: Message2

You get the idea. I have the basic idea. When each message gets added to the MySQL database, it's given a unique ID. If a message is a reply to another message, it's assigned another ID based on the message it's a reply of, and so forth. Now I've figured out a way to have my replies go up to three threads deep, but it's a very inefficient method. For each level of replies, I have to write a separate loop, so it starts looking like:

while($row = mysql_fetch_array($result))
{
**Code to display message**
mysql_query;
while($row2 = mysql_fetch_array($result2))
{
**Code to display message**
mysql_query;
while($row3 = mysql_fetch_array($result3))
{
**code**
}
}
}

You get the idea. This code is very inefficient and slow, and tends to get unstable after about four loops deep.

So all in all my question is: Is there any way to do what I'm trying to do in an efficient manner? I can't figure out the programming logic necessary. Any help would be greatly appreciated. I can furnish the source code if needed.

Thanks in advance.

Re: Nested message board project, I need a hint or two

Posted: Wed Jan 14, 2009 1:48 am
by requinix
Recursion. It's basically a way of nesting a loop to an infinite depth. Or at least as far as the machine can go.

Write a function to find all posts that are the child of some other post. Then keep calling it over and over again.
To keep track of indentation you need another variable besides what the parent post is.

Example:

Code: Select all

function printPostTitles($parent = 0, $indent = "") {
    $query = mysql_query("SELECT * FROM posts WHERE parent = $parent");
    while ($line = mysql_fetch_array($query)) {
        echo $indent, " ", $line["title"], "\n"; // show post
        printPostTitles($line["id"], "*" . $indent); // show children, indent with a *
    }
}
 
printPostTitles();

Re: Nested message board project, I need a hint or two

Posted: Wed Jan 14, 2009 1:54 am
by it2051229
I never tried this before where you get to arrange a reply inside a reply inside a reply inside a reply to the Nth degree. You might want to do this using recursion manner.

Re: Nested message board project, I need a hint or two

Posted: Wed Jan 14, 2009 2:15 am
by Yevgeni
tasairis, you're a savior!

I was thinking the answer laid somewhere in writing a function. I'll work on this a bit and let you know what I come up with.