1 (edited by creaturecorp 2005-09-27 12:45)

Topic: Threaded/Nested Messaging

I've got a mysql database that has a comments table in it, with info my_parent and id. From this info, it's possible to call a recursive PHP function which causes the list to be spit out.

Now here's the one problem: this can take a megaload of queries since a new one is made inside each loop. Is this normal and should I just forget about it, or should I strive to design a system that nests the comments with only one loop and therefore one query?

~Creaturecorp

I don't HAVE a signature, ok?

2

Re: Threaded/Nested Messaging

I am not sure what exactly you mean.

Do you mean you have a table with, say, articles and another table with comments on articles?
In this case you will have a field article_id in your article table and one article_id in your comments table.
So all you do is make a query like this

select * from articles, comments where articles.article_id=comments.article_id
The German PunBB Site:
PunBB-forum.de

Re: Threaded/Nested Messaging

I should have explained more clearly. tongue

I have a mysql table with the column names 'id' and 'my_parent'. The first comment always has a my_parent of 0 and an auto-incremented id. When someone replies to said comment, my_parent of the new comment is the id of the one it's in response to. (say that ten times fast)

With this information, a recursive function can be called to spit out the comments.

something like

function comment_list($id){
//get comment information for comment with the value $id with a query containing  the count() command

$query= $db->query("SELECT *, count(blah) AS replies FROM comments ...blah blah blah");

while ($sb = $db->fetch_assoc($query)){

   //count the number of times the comment's id appears in other comment's my_parent field. Also echo the information back.

   if($sb['replies']>0){
      //add some padding to the next recursion

      echo '<div style="padding-left:8px;">';
      comment_list($newid);
      echo '</div>';

   }

}//end loop

}//end function

comment_list(1);

I hope that makes some sense. big_smile

I don't HAVE a signature, ok?

4 (edited by Tobi 2005-09-29 17:10)

Re: Threaded/Nested Messaging

Ah. That was the other interpretation that I hoped wasn't it smile

An easier way to solve this would be:
Forget about the auto-increment.
Get the highest ID before insertion, add 1 and give this id to the new entry for the ID field and the parent field if it's the first in thread. The replies will get that id as parent as well. So all comments in one threads have the same parent ID. Clear?

Now you can use one query, sort by parent and id.
For each row, keep the last id and parent for comparison.
Then create a variable for the padding space.
Now, if in one row the new parent is the ID of the last row then you know you have to add another padding divs to your output.
As long as the parents stay identical keep the number of padding divs.
If the next parent is different without being the id of the previous row then substract one padding div.

Now, ain't that easy? smile

OK OK, I'll try to write it down but don't blame me if it doesn't work 100%, it's just a sample for what I mean

//Get the highest id
$highest= $db->query("SELECT max(id) as MAXID FROM comments");
$mm = $db->fetch_assoc($highest);
$max = ($mm['MAXID'] + 1);
// Insert one row with a thread starter
$newrow = $db->query("insert into comments (id, parent, text) values (" . $max .", " . $max . ", '" . $text . "'");

//Now get the threads
$all = $db->query("select * from comments order by parent, id");
$padwidth = 8;
while ($sb = $db->fetch_assoc($all)){
  if($all['parent'] == $oldid)  {
    $padwidth += 8;
    $oldparent = $all['parent'];
    }
  elseif($all['parent'] != $oldparent)  {
    $padwidth -= 8;
    }
  echo '<div style="padding-left:' . $padwidth . 'px;">' . $text;
  $oldid = $all['id'];

  
}

Something like this. No warranty. Just a rough sketch... smile

The German PunBB Site:
PunBB-forum.de

Re: Threaded/Nested Messaging

Whoa, sounds like it would work perfectly!!!

I think from there, to limit the number of base comments, all I would have to do is

if($padwidth == 0)
$cmt_count++;

if($cmt_count > $limit)
break;

Thanks for the help, I'll go try it out as soon as possible!!

I don't HAVE a signature, ok?

Re: Threaded/Nested Messaging

It didn't really work... I think your code was supposed to have $sb instead of $all in the loop. I'm needing one that threads them in such a way that there can be more than one comment on a single level. I think this can only be achieved with a recursive function... is there anything wrong with having over 40 queries? Probably...

This is difficult since I have a nested comments script that works, but the only problem is the number of queries.

Tobi, if you can come up with one that doesn't use a crapload of queries and still is completely functional (with ordering ASC and DESC by date) perhaps I can offer you something for it.

I don't HAVE a signature, ok?

7

Re: Threaded/Nested Messaging

You are right about the $sb.
I told you, a rough sketch it was...

Otherwise it should work, I cannot see a problem.
If you can't get it running send me a dump of the structure and at least 20,30 entries to play with so I don't have to make a dummy database. This will take the most time for solving this I assume.

The German PunBB Site:
PunBB-forum.de

Re: Threaded/Nested Messaging

CREATE TABLE `newcomments` (
  `id` int(100) NOT NULL default '0',
  `parent` int(100) NOT NULL default '0',
  `text` text NOT NULL,
  `date` int(150) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM;

I had earlier removed the entries, but it would seem that it's easy to insert new ones.

I don't HAVE a signature, ok?

9 (edited by Tobi 2005-10-01 19:21)

Re: Threaded/Nested Messaging

Some sample entries please?
Like 20, 30?

Because really, that *is* the main work.

The German PunBB Site:
PunBB-forum.de

Re: Threaded/Nested Messaging

I've since changed the script, but I suppose I can get some sample entries. Just to clarify, you would like 20 to 30 sample entries to test with?

I don't HAVE a signature, ok?

11

Re: Threaded/Nested Messaging

Yes,
typing all the dummy entries is totally boring and takes too long.. smile
So, table structure (which I have now) and 20-30 sets of data (representative if possible, yu know, with nested levels, answers to answers and so on...) would be kind.

The German PunBB Site:
PunBB-forum.de

12 (edited by creaturecorp 2005-10-05 21:34)

Re: Threaded/Nested Messaging

Ok, I will create those and get back to you.

I don't HAVE a signature, ok?

Re: Threaded/Nested Messaging

I've done it... it's fully functional. Thanks for the help tobi but it turned out that i didn't need it after all. tongue

I don't HAVE a signature, ok?