当前位置:网站首页>Learn to use PHP to implement unlimited comments and unlimited to secondary comments solutions
Learn to use PHP to implement unlimited comments and unlimited to secondary comments solutions
2022-06-24 10:24:00 【Wandering in memory of Yu Fei】
Learn to use php Solutions for unlimited comments and unlimited to secondary comments
Comment array
$parent_comment_id = 317;
$comment_str = '[{"comment_id":326,"parent_comment_id":317,"reply_comment_id":319,"like_number":0,"is_like":0,"comment_time":"2022-06-02 22:25:41","user_name":" Weirdo 250","comment_user_header":"https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw\/132","comment_user_id":14583,"department_name":" QIPA tiandi.com headquarters • Weirdo 250 Ministry ","comment_content":" You have too many words You're right "},{"comment_id":325,"parent_comment_id":317,"reply_comment_id":317,"like_number":0,"is_like":0,"comment_time":"2022-06-02 22:01:28","user_name":" Weirdo 250","comment_user_header":"https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw\/132","comment_user_id":14583,"department_name":" QIPA tiandi.com headquarters • Weirdo 250 Ministry ","comment_content":" good Continue to work hard "},{"comment_id":324,"parent_comment_id":317,"reply_comment_id":319,"like_number":0,"is_like":0,"comment_time":"2022-06-02 21:46:43","user_name":" Weirdo 250","comment_user_header":"https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw\/132","comment_user_id":14583,"department_name":" QIPA tiandi.com headquarters • Weirdo 250 Ministry ","comment_content":" I think you're right "},{"comment_id":319,"parent_comment_id":317,"reply_comment_id":318,"like_number":0,"is_like":0,"comment_time":"2022-05-26 22:47:55","user_name":" Weirdo 250","comment_user_header":"https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw\/132","comment_user_id":14583,"department_name":" QIPA tiandi.com headquarters • Weirdo 250 Ministry ","comment_content":" Third level comment "},{"comment_id":318,"parent_comment_id":317,"reply_comment_id":317,"like_number":0,"is_like":0,"comment_time":"2022-05-26 22:45:20","user_name":" Weirdo 250","comment_user_header":"https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw\/132","comment_user_id":14583,"department_name":" QIPA tiandi.com headquarters • Weirdo 250 Ministry ","comment_content":" Comments in comments "}]';
$wenku_comment_tmp = json_decode($comment_str, true);
echo "<pre>";
print_r($wenku_comment_tmp);
$wenku_comment_ay_tmp = [];
// You don't have to query the database every time
foreach ($wenku_comment_tmp as $k => $v) {
$wenku_comment_ay_tmp[$v['comment_id']] = $v;
}
Limitless comments
// Comment details
function getWenkuCommentDetailAll($wenku_comment_ay_tmp, $data, $parent_comment_id)
{
$wenku_comment_list = [];
foreach ($data as $k => $v) {
// If a higher level comment id And the parent id Agreement Then put it into the child
if ($parent_comment_id == $v['reply_comment_id']) {
$reply_comment_user = $reply_tip = '';
// If the parent id And reply id, If it is not the same, you need to add reply word
if ($v['parent_comment_id'] != $v['reply_comment_id']) {
$reply_tip = ' reply ';
$reply_comment_user = $wenku_comment_ay_tmp[$v['reply_comment_id']]['user_name'];
}
$child_list = getWenkuCommentDetailAll($wenku_comment_ay_tmp, $data, $v['comment_id']);
// Comment on children , Sort
if ($child_list) {
$comment_id = array_column($child_list, 'comment_id');
array_multisort($comment_id, SORT_ASC, $child_list);
}
$wenku_comment_list[] = array(
'comment_id' => $v['comment_id'],
'parent_comment_id' => (int)$v['parent_comment_id'],
'reply_comment_id' => (int)$v['reply_comment_id'],
'reply_comment_user' => $reply_comment_user,
'reply_tip' => $reply_tip,
'like_number' => (int)$v['like_number'],// Number of likes
'is_like' => (int)$v['is_like'],// Did you like it
'comment_time' => $v['comment_time'],// Comment date
'user_name' => $v['user_name'],
'department_name' => $v['department_name'],// Reviewer's Department
'comment_content' => $v['comment_content'],// Comment content
'comment_user_id' => (int)$v['comment_user_id'],// Comment users id
'comment_user_header' => $v['comment_user_header'],// Commenter's Avatar
'list' => $child_list ?: [],
);
unset($reply_tip, $reply_comment_user, $child_list);
}
}
return $wenku_comment_list;
}
$company_user_comment_list = getWenkuCommentDetailAll($wenku_comment_ay_tmp, $wenku_comment_tmp, $parent_comment_id);
echo " Unlimited company_user_comment_list<pre>";
print_r($company_user_comment_list);
Print the results
Unlimited company_user_comment_list
Array
(
[0] => Array
(
[comment_id] => 325
[parent_comment_id] => 317
[reply_comment_id] => 317
[reply_comment_user] =>
[reply_tip] =>
[like_number] => 0
[is_like] => 0
[comment_time] => 2022-06-02 22:01:28
[user_name] => Weirdo 250
[department_name] => QIPA tiandi.com headquarters • Weirdo 250 Ministry
[comment_content] => good Continue to work hard
[comment_user_id] => 14583
[comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
[list] => Array
(
)
)
[1] => Array
(
[comment_id] => 318
[parent_comment_id] => 317
[reply_comment_id] => 317
[reply_comment_user] =>
[reply_tip] =>
[like_number] => 0
[is_like] => 0
[comment_time] => 2022-05-26 22:45:20
[user_name] => Weirdo 250
[department_name] => QIPA tiandi.com headquarters • Weirdo 250 Ministry
[comment_content] => Comments in comments
[comment_user_id] => 14583
[comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
[list] => Array
(
[0] => Array
(
[comment_id] => 319
[parent_comment_id] => 317
[reply_comment_id] => 318
[reply_comment_user] => Weirdo 250
[reply_tip] => reply
[like_number] => 0
[is_like] => 0
[comment_time] => 2022-05-26 22:47:55
[user_name] => Weirdo 250
[department_name] => QIPA tiandi.com headquarters • Weirdo 250 Ministry
[comment_content] => Third level comment
[comment_user_id] => 14583
[comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
[list] => Array
(
[0] => Array
(
[comment_id] => 324
[parent_comment_id] => 317
[reply_comment_id] => 319
[reply_comment_user] => Weirdo 250
[reply_tip] => reply
[like_number] => 0
[is_like] => 0
[comment_time] => 2022-06-02 21:46:43
[user_name] => Weirdo 250
[department_name] => QIPA tiandi.com headquarters • Weirdo 250 Ministry
[comment_content] => I think you're right
[comment_user_id] => 14583
[comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
[list] => Array
(
)
)
[1] => Array
(
[comment_id] => 326
[parent_comment_id] => 317
[reply_comment_id] => 319
[reply_comment_user] => Weirdo 250
[reply_tip] => reply
[like_number] => 0
[is_like] => 0
[comment_time] => 2022-06-02 22:25:41
[user_name] => Weirdo 250
[department_name] => QIPA tiandi.com headquarters • Weirdo 250 Ministry
[comment_content] => You have too many words You're right
[comment_user_id] => 14583
[comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
[list] => Array
(
)
)
)
)
)
)
)
Second level comment
Change all comments above level 3 into Level 2 comments
function getlist($wenku_comment_ay_tmp, $list)
{
$tmp = [];
foreach ($list as $k => $v) {
if ($v['reply_comment_id'] == 317) {
$child_coment = getsubtree($wenku_comment_ay_tmp, $list, $v['comment_id']);
$v['child'] = $child_coment;
$tmp[] = $v;
unset($child_coment);
}
}
return $tmp;
}
function getsubtree($wenku_comment_ay_tmp, $list, $pid)
{
$tmp = [];
foreach ($list as $lk => $lv) {
if ($lv['reply_comment_id'] == $pid) {
unset($list[$lk]);
$reply_comment_user = $reply_tip = '';
// If the parent id And reply id, If it is not the same, you need to add reply word
if ($lv['parent_comment_id'] != $lv['reply_comment_id']) {
$reply_tip = ' reply ';
$reply_comment_user = $wenku_comment_ay_tmp[$lv['reply_comment_id']]['user_name'];
}
// Add fields
$lv['reply_comment_user'] = $reply_comment_user;
$lv['reply_tip'] = $reply_tip;
$tmp[] = $lv;
$tmplist = getsubtree($wenku_comment_ay_tmp, $list, $lv['comment_id']);
$tmp = array_merge($tmp, $tmplist);
}
}
return $tmp;
}
$company_user_comment_list = getlist($wenku_comment_ay_tmp, $wenku_comment_tmp);
echo " Level two company_user_comment_list<pre>";
print_r($company_user_comment_list);
Print the results
Level two company_user_comment_list
Array
(
[0] => Array
(
[comment_id] => 325
[parent_comment_id] => 317
[reply_comment_id] => 317
[like_number] => 0
[is_like] => 0
[comment_time] => 2022-06-02 22:01:28
[user_name] => Weirdo 250
[comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
[comment_user_id] => 14583
[department_name] => QIPA tiandi.com headquarters • Weirdo 250 Ministry
[comment_content] => good Continue to work hard
[child] => Array
(
)
)
[1] => Array
(
[comment_id] => 318
[parent_comment_id] => 317
[reply_comment_id] => 317
[like_number] => 0
[is_like] => 0
[comment_time] => 2022-05-26 22:45:20
[user_name] => Weirdo 250
[comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
[comment_user_id] => 14583
[department_name] => QIPA tiandi.com headquarters • Weirdo 250 Ministry
[comment_content] => Comments in comments
[child] => Array
(
[0] => Array
(
[comment_id] => 319
[parent_comment_id] => 317
[reply_comment_id] => 318
[like_number] => 0
[is_like] => 0
[comment_time] => 2022-05-26 22:47:55
[user_name] => Weirdo 250
[comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
[comment_user_id] => 14583
[department_name] => QIPA tiandi.com headquarters • Weirdo 250 Ministry
[comment_content] => Third level comment
[reply_comment_user] => Weirdo 250
[reply_tip] => reply
)
[1] => Array
(
[comment_id] => 326
[parent_comment_id] => 317
[reply_comment_id] => 319
[like_number] => 0
[is_like] => 0
[comment_time] => 2022-06-02 22:25:41
[user_name] => Weirdo 250
[comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
[comment_user_id] => 14583
[department_name] => QIPA tiandi.com headquarters • Weirdo 250 Ministry
[comment_content] => You have too many words You're right
[reply_comment_user] => Weirdo 250
[reply_tip] => reply
)
[2] => Array
(
[comment_id] => 324
[parent_comment_id] => 317
[reply_comment_id] => 319
[like_number] => 0
[is_like] => 0
[comment_time] => 2022-06-02 21:46:43
[user_name] => Weirdo 250
[comment_user_header] => https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKFgDdZvicmXAdpOtL08FFapdVW4KTibSPAjSd3wOS5WgJjJIwxpjlAiarD4U9jw3rOKAVorV1xtW1Iw/132
[comment_user_id] => 14583
[department_name] => QIPA tiandi.com headquarters • Weirdo 250 Ministry
[comment_content] => I think you're right
[reply_comment_user] => Weirdo 250
[reply_tip] => reply
)
)
)
)
边栏推荐
- 411 stack and queue (20. valid parentheses, 1047. delete all adjacent duplicates in the string, 150. inverse Polish expression evaluation, 239. sliding window maximum, 347. the first k high-frequency
- 正规方程、、、
- Safety and food security for teachers and students of the trapped Yingxi middle school
- [db2] sql0805n solution and thinking
- 2.登陆退出功能开发
- Leetcode-1089: replication zero
- 1.项目环境搭建
- H5网页如何在微信中自定义分享链接
- uniapp开发微信小程序,显示地图功能,且点击后打开高德或腾讯地图。
- 自定义kindeditor编辑器的工具栏,items即去除不必要的工具栏或者保留部分工具栏
猜你喜欢

大中型企业如何构建自己的监控体系

411 stack and queue (20. valid parentheses, 1047. delete all adjacent duplicates in the string, 150. inverse Polish expression evaluation, 239. sliding window maximum, 347. the first k high-frequency

线程的 sleep() 方法与 wait() 方法的区别

Leetcode-2221: triangular sum of arrays

6. package management business development

线程池的执行流程

6.套餐管理业务开发

The difference between the sleep () method and the wait () method of a thread

3.员工的增删改查

线程的六种状态
随机推荐
leetCode-1089: 复写零
正规方程、、、
SQL sever基本数据类型详解
canvas管道动画js特效
leetCode-面试题 16.06: 最小差
线程的 sleep() 方法与 wait() 方法的区别
Internet of things? Come and see Arduino on the cloud
[EI分享] 2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)
uniapp实现禁止video拖拽快进
百度网盘下载一直请求中问题解决
Common methods of thread scheduling
消息队列的作用
CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance
形状变化loader加载jsjs特效代码
Leetcode-1089: replication zero
np.float32()
Caching mechanism for wrapper types
线程池的执行流程
解决微信小程序rich-text富文本标签内部图片宽高自适应的方法
Safety and food security for teachers and students of the trapped Yingxi middle school