
June 25th, 2004, 10:27 AM
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
PHP / MySQL Threaded Discussion: One Query
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_created).
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?
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?
PHP Code:
[color=#000000]///============
//Sample thread
$thread_info_arr['message_id'] = array(154, 155, 156, 157, 161, 163);
$thread_info_arr['reply_id'] = array(0, 154, 155, 154, 155, 157);
$thread_info_arr['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_arr['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($thread_info_arr['message_id']);
function reord_thread(&$data)
{
global $thread_info_arr, $iteration;
if (is_array($data))
{
array_walk($data, 'reord_thread');
}
else
{
echo 'Date: ' . $thread_info_arr['date_posted'][$iteration] . ' Title: ' . $thread_info_arr['message_title'][$iteration] . '<br>';
$iteration++;
}
}[/color]
///============
Thanks in advance. Any help would be greatly appreciated. Enjoy your day.
|