Page 1 of 1

need help, loops and ifs

Posted: Fri Aug 11, 2006 8:44 am
by davey_s
Hi hows it going.

I am looking to create my own forum page.
Each thread has an id, and each reply is a thread, only the difference is they have a "parent_id".

i want the code to check if the thread has any replies eg select * where parent_id = thread_id

this is all working fine. however, i want to have this recurring so that the code keep checking for EVERY reply, to see if this thread_id has any replies, so that the structure is like an endless loop, giving case or switches i think.

so a forum would look like this:

dave - 13/5/67
___reply - dave2 - 13/5/67
___reply - dave3 - 13/5/67
___reply - dave5 - 13/5/67
_______another reply - dave8 - 13/5/06
_______another reply - dave8 - 13/5/06
_____________and another reply - dave2 - 13/12/06
_______another reply - dave8 - 13/5/06
_______another reply - dave13 - 13/5/06
_____________and another reply - dave2 - 13/12/06

Can someone tell me where to start? i have looked into functions and stuff but cant seem to find they technically correct way of doing this.

Posted: Fri Aug 11, 2006 8:52 am
by s.dot
Normally what is done is the reply count is stored in the thread table. When a new post is made, this count is updated. When a post is deleted, this count is decremented.

Also, what could be done is selecting a count using a query.

Code: Select all

$count = mysql_query("SELECT count(*) FROM `my_table` WHERE `topicid` = '$topicid'") or die(mysql_error());
$count = mysql_result($count,0);
echo $count;

Posted: Fri Aug 11, 2006 9:00 am
by davey_s
i understand how to cound the replies... but how do you structure it so that it indefinately will repeat the query at the end of each thread?

eg.

check for thread
---------> post thread
-------------- > check for replies
------------------> if replies > 0 post replies
----------------------->check for replies of replies
------------------------------> if replies > 0 post replies
-------------------------------->check replies of replies of replies
---------------------------------->if replies > 0 post replies

etc....

Posted: Fri Aug 11, 2006 10:03 am
by feyd
Since you've chosen to go the threaded approach to creating a forum, you may want to look into making the database structure support hierarchical trees.

http://www.sitepoint.com/print/1105