Jump to content

[SOLVED] Can't get my head round this


almightyegg

Recommended Posts

This could be a very simple thing which I'm just missing but here it goes...

 

I'm making a threaded message board. Posts work like this:

MAIN MESSAGE

reply 1

  reply to reply 1(reply 3)

      reply to reply(reply 6)

reply 2

  reply to reply 2(reply5)

  another reply to reply 2(reply4)

 

The way this is set out in my database is ALL in one table, `messages`.

 

The columns that are required for this to work are

messageid - unique id of the post

pid - the id of the MAIN MESSAGE (if it is a main message it's 0)

rid - the id of the post it is directly replying to, so in the example above reply 1 would have the same pid and rid as it's main message is also the one it's directly replying to. reply5's rid would be the id of reply2 and so forth.

 

I hope that all makes sense...

 

 

My brainteaser is that when somebody deletes a message I want it to delete all the replies underneath it...

so if I were to delete reply 1 it would delete reply 3 and 6 also.

 

I have been toying with some codes but it always ends out that I need a never ending set of while loops....

 

eg while{

while{

while{

etc. for ever

}}}

 

Any ideas of better ways to set this up or ideas to solve the problem would be greatly appreciated :)

 

Thanks

Link to comment
Share on other sites

You may want to read up on hiearchical structures, there are plenty of examples online.

 

I usually use a simple solution where I store a "hierarchy string" in the database, for example 4 < 5 < 6 < 7. This means that this particular message (7) is a child of 6 which in turn is a child of 5, etc. It makes it easy to delete all descendants. If you wish to use a string for sorting you will need to use the correct data though and the model is not really scalable for large systems.

 

The only thing you need to think about is to user a separator that should never be in the value list. The system does have limitations though which is why it is not always a good solution. You will find other alternatives when searching as suggested above.

Link to comment
Share on other sites

That is what the "hierarchy string" is for. That way you can easily delete all descendants of that message. Wasn't that your main obstacle?

 

Btw, what you mean "a hierarchy system" wouldn't work? This is exactly what they are made for... They do not require many loops.

Link to comment
Share on other sites

The idea that involved the never ending loops would be something like this:

get the id of the deleted post. call this $id

 

get any posts with rid = $id

while out these posts

call their ids $id2

 

find any posts with $id2 as rid

while out these posts

call their ids $id3

 

etc..

 

and once you have them all delete

problem is, it could have any number of replies so there's no definite way working out how many needed...

 

 

Is there a way this can be done in a function? If somebody can point me in the right direction that'd be great

Link to comment
Share on other sites

Did you even care to read and think about what I described above concerning the "hierarchy string"? You would use that when deleting in that case, not your rids all along the way. That is not an alternative. Using my suggested method you will only have to do one single sql query instead of countless loops.

 

Either that or use real hierarchical structures for which you will find all the information you need on deletes and inserts if you let Google do your job.

Link to comment
Share on other sites

But surely the hierarchical string setup wouldn't work?

 

if you have a system like mine how would you number them??

 

MAIN MESSAGE - 1

reply 1 - 2

  reply to reply 1(reply 3) - 3

        reply to reply(reply 6) - 4

reply 2 - 2

  reply to reply 2(reply5) - 3

  another reply to reply 2(reply4) - 3

 

Like this?? If so then you still have to find if they're replies to that thread as there are other numbers the same, so I'd still have the while problem

Link to comment
Share on other sites

Done it!

 

It took a lot of trial and error but it's working :D

 

here's what I used ($_POST[select] is an array based on the id of selected by checkbox posts to delete)

<?
include 'db.php';
$post = $_POST['select'];
if($post == ''){
header("Location: forum.php");
}else{

$d = $post;
$r = $post;
while($r <> 0){

$yum = implode(",", $r);

$sel = mysql_query("SELECT * FROM messages WHERE `rid` in ($yum)") or die(mysql_error());
if(mysql_num_rows($sel) == 0){
$r = 0;
}else{
$r = array();
while($det = mysql_fetch_array($sel)){
$d[] = $det[messageid];
$r[] = $det[messageid];

}
}

}

$delete = implode(",", $d);

mysql_query("DELETE FROM `messages` WHERE `messageid` in ($delete)");
header("Location: forum.php");
}
?>

 

Thanks for the help

Link to comment
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.