I'm attempting to create a threaded comment system involving PHP /
MySQL. Currently, I have a table called comments which looks like:
Table Comments: (comment_id, comment_root_id , comment_parent_ id, text,
datetime_create d).
Most of the tutorials that I've found online discuss how to thread the
results from the database with recursive query calls. This is all to
the good; however, with lengthy discussion, this solution becomes slow
and unacceptable.
Is there a way to retrieve all the comments from the database in one
query (e.g. SELECT * FROM comments WHERE comment_root_id = $id) and
then to do the ordering/threading in php with some function (i'm
guessing recursive to handle infinite depths)?
What would such a function look like?
I found a function online that recurses through an array. Perhaps
something like this could be modified to thread the comments?
///============
//Sample thread
$thread_info_ar r['message_id'] = array(154, 155, 156, 157, 161, 163);
$thread_info_ar r['reply_id'] = array(0, 154, 155, 154, 155, 157);
$thread_info_ar r['message_title'] = array('Post 154 (initial post)',
'Post 155 (reply to post 154)', 'Post 156 (reply to post 155)', 'Post
157 (reply to post 154)', 'Post 161 (reply to post 155)', 'Post 163
(reply to post 157)');
$thread_info_ar r['date_posted'] = array('2004-06-01', '2004-06-02',
'2004-06-03', '2004-06-04', '2004-06-05', '2004-06-06');
$iteration = 0;
reord_thread($t hread_info_arr['message_id']);
function reord_thread(&$ data)
{
global $thread_info_ar r, $iteration;
if (is_array($data ))
{
array_walk($dat a, 'reord_thread') ;
}
else
{
echo 'Date: ' . $thread_info_ar r['date_posted'][$iteration] . ' Title:
' . $thread_info_ar r['message_title'][$iteration] . '<br>';
$iteration++;
}
}
///============
Thanks in advance. Any help would be greatly appreciated. Enjoy your
day.
MySQL. Currently, I have a table called comments which looks like:
Table Comments: (comment_id, comment_root_id , comment_parent_ id, text,
datetime_create d).
Most of the tutorials that I've found online discuss how to thread the
results from the database with recursive query calls. This is all to
the good; however, with lengthy discussion, this solution becomes slow
and unacceptable.
Is there a way to retrieve all the comments from the database in one
query (e.g. SELECT * FROM comments WHERE comment_root_id = $id) and
then to do the ordering/threading in php with some function (i'm
guessing recursive to handle infinite depths)?
What would such a function look like?
I found a function online that recurses through an array. Perhaps
something like this could be modified to thread the comments?
///============
//Sample thread
$thread_info_ar r['message_id'] = array(154, 155, 156, 157, 161, 163);
$thread_info_ar r['reply_id'] = array(0, 154, 155, 154, 155, 157);
$thread_info_ar r['message_title'] = array('Post 154 (initial post)',
'Post 155 (reply to post 154)', 'Post 156 (reply to post 155)', 'Post
157 (reply to post 154)', 'Post 161 (reply to post 155)', 'Post 163
(reply to post 157)');
$thread_info_ar r['date_posted'] = array('2004-06-01', '2004-06-02',
'2004-06-03', '2004-06-04', '2004-06-05', '2004-06-06');
$iteration = 0;
reord_thread($t hread_info_arr['message_id']);
function reord_thread(&$ data)
{
global $thread_info_ar r, $iteration;
if (is_array($data ))
{
array_walk($dat a, 'reord_thread') ;
}
else
{
echo 'Date: ' . $thread_info_ar r['date_posted'][$iteration] . ' Title:
' . $thread_info_ar r['message_title'][$iteration] . '<br>';
$iteration++;
}
}
///============
Thanks in advance. Any help would be greatly appreciated. Enjoy your
day.
Comment