patternphpMinor
Displaying comments and replies retrieved from a database
Viewed 0 times
retrieveddisplayingdatabaserepliesandfromcomments
Problem
I created an example (for me) of how I must get the comments and replies from the database.
Here is the result for example:
```
//I am ONLY thinking a query like this
$q = "SELECT * FROM comments_table WHERE post = 'url_code' ORDER BY date DESC";
//An example result (Not used the query above)
$comment = array(
array(
'id' => '7',
'user' => 'Jul',
'post_id' => '1',
'comment_id' => '7',
'reply_id' => '1',
'date' => 'Jul 2016',
'message' => 'hello7'
),
array(
'id' => '6',
'user' => 'Jun',
'post_id' => '1',
'comment_id' => '6',
'reply_id' => '1',
'date' => 'Jun 2016',
'message' => 'hello6'
),
array(
'id' => '5',
'user' => 'May',
'post_id' => '1',
'comment_id' => '5',
'reply_id' => '3',
'date' => 'May 2016',
'message' => 'hello5'
),
array(
'id' => '4',
'user' => 'Apr',
'post_id' => '1',
'comment_id' => '4',
'reply_id' => '2',
'date' => 'Apr 2016',
'message' => 'hello4'
),
array(
'id' => '3',
'user' => 'Mar',
'post_id' => '1',
'comment_id' => '3',
'reply_id' => '',
'date' => 'Mar 2016',
'message' => 'hello3'
),
array(
'id'
Here is the result for example:
```
//I am ONLY thinking a query like this
$q = "SELECT * FROM comments_table WHERE post = 'url_code' ORDER BY date DESC";
//An example result (Not used the query above)
$comment = array(
array(
'id' => '7',
'user' => 'Jul',
'post_id' => '1',
'comment_id' => '7',
'reply_id' => '1',
'date' => 'Jul 2016',
'message' => 'hello7'
),
array(
'id' => '6',
'user' => 'Jun',
'post_id' => '1',
'comment_id' => '6',
'reply_id' => '1',
'date' => 'Jun 2016',
'message' => 'hello6'
),
array(
'id' => '5',
'user' => 'May',
'post_id' => '1',
'comment_id' => '5',
'reply_id' => '3',
'date' => 'May 2016',
'message' => 'hello5'
),
array(
'id' => '4',
'user' => 'Apr',
'post_id' => '1',
'comment_id' => '4',
'reply_id' => '2',
'date' => 'Apr 2016',
'message' => 'hello4'
),
array(
'id' => '3',
'user' => 'Mar',
'post_id' => '1',
'comment_id' => '3',
'reply_id' => '',
'date' => 'Mar 2016',
'message' => 'hello3'
),
array(
'id'
Solution
It seems you wish to batch the posted messages together into threads. In other words, create a "parent - children" relationship. This can be achieved simply by using one loop to generate the new data structure and one smaller loop (I use
Code: (Demo)
Output (same as your posted code provides):
Some additional notes:
-
Using
-
There appears to be data redundancy with
-
A final consideration... when dealing with thread-based messages/posts/data, it simplifies tasks like these to assign a
array_map()) to display the text.Code: (Demo)
foreach ($comments as $data) {
if ($data['reply_id']!='') {
$threads[$data['reply_id']][]="{$data['user']} reply: {$data['message']}"; // append element
} else {
if (!isset($threads[$data['id']])) { // prevent Notice
$threads[$data['id']] = [];
}
array_unshift($threads[$data['id']], "{$data['user']} comment: {$data['message']}"); // prepend element
}
}
// display data (with no unwanted line breaks):
echo implode("", array_map(function($v){return implode("",$v);}, $threads));Output (same as your posted code provides):
Jan comment: hello
Jul reply: hello7
Jun reply: hello6
Mar comment: hello3
May reply: hello5
Feb comment: hello2
Apr reply: hello4Some additional notes:
-
Using
ORDER BY date DESC will place newer replies above older replies, be sure that this makes sense in terms of discussion "flow".-
There appears to be data redundancy with
id and comment_id. If there is a difference between these two columns of data that doesn't present itself in your posted task, then they both can stay, otherwise you should omit one of these columns from your database.-
A final consideration... when dealing with thread-based messages/posts/data, it simplifies tasks like these to assign a
thread_id, then you can GROUP BY and/or ORDER BY the column in your SQL and spare much of the php handling.Code Snippets
foreach ($comments as $data) {
if ($data['reply_id']!='') {
$threads[$data['reply_id']][]="{$data['user']} reply: {$data['message']}"; // append element
} else {
if (!isset($threads[$data['id']])) { // prevent Notice
$threads[$data['id']] = [];
}
array_unshift($threads[$data['id']], "{$data['user']} comment: {$data['message']}"); // prepend element
}
}
// display data (with no unwanted line breaks):
echo implode("<br><br>", array_map(function($v){return implode("<br>",$v);}, $threads));Jan comment: hello
Jul reply: hello7
Jun reply: hello6
Mar comment: hello3
May reply: hello5
Feb comment: hello2
Apr reply: hello4Context
StackExchange Code Review Q#155196, answer score: 2
Revisions (0)
No revisions yet.