need help, loops and ifs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davey_s
Forum Newbie
Posts: 2
Joined: Fri Aug 11, 2006 8:35 am

need help, loops and ifs

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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;
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
davey_s
Forum Newbie
Posts: 2
Joined: Fri Aug 11, 2006 8:35 am

Post 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....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
Post Reply