Forum Moderators: coopster

Message Too Old, No Replies

Journey into recursive hell.

         

wavesurf

10:54 am on Oct 10, 2005 (gmt 0)

10+ Year Member



I'm trying to make a forum with a recursive script showing the entire discussion thread, but I find it impossible.

In my database table I have a messageParentId that shows if this is a new message thread (id=0) or an answer to an existing message (id=zzzzz). I have managed to make a script part that goes back or forth all the way in one level, but I don't know how to make a script that gives me all subsequent answers if a message has more than one answer.

Any one know how to get me out of recursive hell?

Thanks for any help...

coopster

11:16 pm on Oct 10, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well, first I would assume there must be another column in the table that identifies that message id=0 is in the same thread as message id=zzzzz. Is this true?

wavesurf

1:02 pm on Oct 11, 2005 (gmt 0)

10+ Year Member



Well, no there isn't. But it shouldn't be a problem implementing this, if that will help.

How can this be put to use, if every message have an originId and a parentId?

Thanks for any help.

Bowtome

2:31 pm on Oct 11, 2005 (gmt 0)



What I would do.

In my table have

newmessage = 1 or 0 for yes or no
count1 = count of new messages
uniqeid = if new message is 1 then uniqueid = count1+1

If a reply to uniqeid, then newmessage = 0 and uniqueid is same as message responded to.

Then print out message as main topic if newmessage = 1.
Then print out all messages where newmessage =0 and uniqueid is same as where newmessage is 1 and uniqueid are the same.

coopster

2:44 pm on Oct 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




every message have an originId and a parentId

Oh, now I think I am beginning to understand your table design. I was wondering how you made the connection/relation of one relative message to the previous and next. So is it something like this?

messageId originId parentId
--------- -------- --------
000000001 00000000 00000003

Can you show an example similar to this that describes the table layout?

wavesurf

9:05 pm on Oct 11, 2005 (gmt 0)

10+ Year Member



Sure:

messageId originId parentId
--------- -------- --------
000000001 00000001 00000000
000000002 00000001 00000001
000000003 00000001 00000001
000000004 00000001 00000002
000000005 00000001 00000002
000000006 00000001 00000002
000000007 00000001 00000001
000000008 00000001 00000005
000000009 00000001 00000008
000000010 00000001 00000009

Explanation:

There is only one thread, and it starts with id: 000000001, messageId 2, 3 and 7 are answers to this message, and 4 ,5 and 6 are answers to id 2. 8 is an answer to 5, 9 is an answer to 8 and 10 is an answer to 9.

Was this what you wanted?

vincevincevince

12:02 am on Oct 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is certainly a case for a single recursive function...

Starting with message $a


print showmessages($a);

function showmessages($id)
{
//first show the message itself
$r=mysql_fetch_array(mysql_query("SELECT * FROM messages WHERE messageId = $id"));
$out.="$r[title] $r[text]<br>";
//now we show the children by getting one at at time
$dh=mysql_query("SELECT * FROM messages WHERE parentId = $id");
while ($r=mysql_fetch_array($dh))
{
//we add these messages to our list
$out.=showmessages($r[messageId]);
}
//return this message, with all its children and grandchildren
return $out;
}

wavesurf

6:28 pm on Oct 12, 2005 (gmt 0)

10+ Year Member



Perfect. That was exactly what I needed and best of all, I understood everything :)

Thanks for all the great help.