almightyegg Posted July 27, 2009 Share Posted July 27, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/167632-solved-cant-get-my-head-round-this/ Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167632-solved-cant-get-my-head-round-this/#findComment-884034 Share on other sites More sharing options...
almightyegg Posted July 27, 2009 Author Share Posted July 27, 2009 I don't think a hierarchy system would work in this case... it would still require many many loops and it's not practical, unless you can explain how it could be used? I could be thinking of it incorrectly Quote Link to comment https://forums.phpfreaks.com/topic/167632-solved-cant-get-my-head-round-this/#findComment-884076 Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167632-solved-cant-get-my-head-round-this/#findComment-884089 Share on other sites More sharing options...
almightyegg Posted July 27, 2009 Author Share Posted July 27, 2009 but I can't see how it would work and how I would code it to delete them? Quote Link to comment https://forums.phpfreaks.com/topic/167632-solved-cant-get-my-head-round-this/#findComment-884097 Share on other sites More sharing options...
almightyegg Posted July 27, 2009 Author Share Posted July 27, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/167632-solved-cant-get-my-head-round-this/#findComment-884201 Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167632-solved-cant-get-my-head-round-this/#findComment-884205 Share on other sites More sharing options...
almightyegg Posted July 27, 2009 Author Share Posted July 27, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/167632-solved-cant-get-my-head-round-this/#findComment-884212 Share on other sites More sharing options...
almightyegg Posted July 27, 2009 Author Share Posted July 27, 2009 Done it! It took a lot of trial and error but it's working 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 Quote Link to comment https://forums.phpfreaks.com/topic/167632-solved-cant-get-my-head-round-this/#findComment-884237 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.