Jump to content

Recommended Posts

Hello everyone

 

I'm trying to make a threaded forum, where each reply sits below the item it was a reply to....(like slashdot.org or b3ta.com.

Problem is I can't figure out a neat way of doing this that doesn't involve one SQL query per item.

My brain's stuck in an infinite loop on this one, can anyone help?

 

Thans in advance!

Link to comment
https://forums.phpfreaks.com/topic/140808-threaded-forum/
Share on other sites

I've only got pseudocode at the moment, but so far i've got something like....

 

posts database:

id, datetime, replyto, userid, content

1, 13/01/2009, NULL, 1, "blah blah blah"

2, 13/01/2009, NULL, 1, "kjhsad"

3, 14/01/2009, 1, 1, "asd asd asd"

 

which i'd like to display as

POST 1

    POST 3(reply to post 1)

POST 2 (new post)

 

 

So far i'm looking at something like....

SELECT * FROM posts WHERE replyto = NULL

foreach{
     echo "content id=1"
     ifexists (posts entry with replyto=1)
          echo "content id=2"
          if exists(posts entry with replyto=2)
          etc....
}

 

But it's getting this bit to loop nicely that I'm having problems with.

I had naively hope to be able to use some kind of recursive function like

 

writePost(){
   echo "post content"
   if (reply exists){
      writePost($replyid);
   }
}

 

but that doesn't appear to be an option and it also involves an SQL query for each entry, which seems a bit excessive.

Link to comment
https://forums.phpfreaks.com/topic/140808-threaded-forum/#findComment-737000
Share on other sites

The kicker is without your table structure it is hard to really help.

 

It would go something like this, you have a thread table. In this you have a parentid set where if this is not 0 then the thread is a reply. You would then send a parent/threadid if that is present you query selecting all rows where parentid is equal to what you sent you would have an OR statement here to OR threadid = passed parent id. Then you would order it by threadid which should put the threads in order, oldest showing first and so on.

 

All done with 1 query.

Link to comment
https://forums.phpfreaks.com/topic/140808-threaded-forum/#findComment-737006
Share on other sites

my table structure is up there ^^

i've called it posts, you've called it threads...i've called it replyto, you've called it parentid.

 

What you've said makes sense, but surely that would only work with one depth? I mean if i have post #1, then post #2 with parentid=1 would both be picked, but if there's a post #3 with parentid=2 that wuoldn't be shown

Link to comment
https://forums.phpfreaks.com/topic/140808-threaded-forum/#findComment-737180
Share on other sites

my table structure is up there ^^

i've called it posts, you've called it threads...i've called it replyto, you've called it parentid.

 

What you've said makes sense, but surely that would only work with one depth? I mean if i have post #1, then post #2 with parentid=1 would both be picked, but if there's a post #3 with parentid=2 that wuoldn't be shown

 

Ah you are talking about actually linking a reply to a reply. You would need a recursive function for that if I am not mistaken, and it would be multiple queries in that case. You could, however, is have a"replyto" and a "originalid" then put the original ID and pull out by that then sort by threadid, replytoid  and that should work.

Link to comment
https://forums.phpfreaks.com/topic/140808-threaded-forum/#findComment-737184
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.