Reputation: 443
Ok, so here is my problem. I am implementing a forum for a client of mine. The code was written years ago and I am working on a delete feature.
Basically, you have your Primary topic. People can comment on that, and comment then on the comments.
The layout would be as such:
[ Main Post ]
[Comment A - ID 1 - Family Founder]
[Comment 2 - Parent 1 - ID 2]
[Comment 3 - Parent 1 - ID 3]
[Comment 4 - Parent 3 - ID 4]
[COmment B - ID 5 - Family Founder]
[Comment 6 - Parent 5 - ID 6]
[Comment 7 - Parent 6 - ID 7]
The table stores this info as follows: ID | Parent ID | Topic ID | Comment Content
Currently, it is setup to run with two functions The first, calls the primary comment (Comment ID 1) and then at the bottom, it calls to see if there are any children of comment ID 1 then loops through them, etc. It is confusing to try to explain.
The problem is, I am unable to keep track of the initial family found (A and B, IDs 1 and 5 respectively).
The only way I can think to do this is to call a function making a multitude of queries on the database until the children count is 0. Storing all the IDs then running a delete query.
Any help is appreciated. If I can explain it better please let me know.
Upvotes: 0
Views: 45
Reputation: 505
I think you should use reoccurrence, e.a. a function that first prints the parrent and then searches all its children and runs himselve for the children. This would mean that you have the following function calls:
Basically something like:
function showPost(int ID) {
#Find record
mysql.runquery ...
#print record
echo '...';
#find children
myql.runquery ...
while (c=mysql.nextrecord()) {
showpost (c.ID);
}
}
For deletion something like:
function deletePost(int ID) {
#find children
myql.runquery ...
while (c=mysql.nextrecord()) {
deletepost (c.ID);
}
#Then delete himselve.
mysql.run_sql ('delete from table where id = '.ID);
}
Upvotes: 0
Reputation: 2239
ParentID
is effectively a foreign key so I'd add a
FOREIGN KEY(ParentID) REFERENCES Comment(id) ON DELETE CASCADE
constraint
Upvotes: 1