mrchimp Posted January 14, 2009 Share Posted January 14, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/140808-threaded-forum/ Share on other sites More sharing options...
premiso Posted January 14, 2009 Share Posted January 14, 2009 Post some code/SQL if you want help. Quote Link to comment https://forums.phpfreaks.com/topic/140808-threaded-forum/#findComment-736979 Share on other sites More sharing options...
mrchimp Posted January 14, 2009 Author Share Posted January 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/140808-threaded-forum/#findComment-737000 Share on other sites More sharing options...
premiso Posted January 14, 2009 Share Posted January 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/140808-threaded-forum/#findComment-737006 Share on other sites More sharing options...
mrchimp Posted January 14, 2009 Author Share Posted January 14, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/140808-threaded-forum/#findComment-737180 Share on other sites More sharing options...
premiso Posted January 14, 2009 Share Posted January 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/140808-threaded-forum/#findComment-737184 Share on other sites More sharing options...
mrchimp Posted January 19, 2009 Author Share Posted January 19, 2009 Thanks for that. If anyone else is having trouble with this I'd recommend this article Quote Link to comment https://forums.phpfreaks.com/topic/140808-threaded-forum/#findComment-740367 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.