当前位置:网站首页>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
)
)
)
)
边栏推荐
- Safety and food security for teachers and students of the trapped Yingxi middle school
- 线程的 sleep() 方法与 wait() 方法的区别
- 5.菜品管理业务开发
- 时尚的弹出模态登录注册窗口
- Resolved: methods with the same name as their class will not be constructors in
- How large and medium-sized enterprises build their own monitoring system
- leetCode-1089: 复写零
- SQL Sever中的窗口函数row_number()rank()dense_rank()
- 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
- p5.js千纸鹤动画背景js特效
猜你喜欢

1. project environment construction

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

希尔排序图文详解+代码实现

numpy.linspace()

Juul, the American e-cigarette giant, suffered a disaster, and all products were forced off the shelves

上升的气泡canvas破碎动画js特效

canvas掉落的小球重力js特效动画

自定义kindeditor编辑器的工具栏,items即去除不必要的工具栏或者保留部分工具栏

How large and medium-sized enterprises build their own monitoring system

leetCode-2221: 数组的三角和
随机推荐
包装类型与基本类型的区别
Using pandas to read SQL server data table
Yolov6: the fast and accurate target detection framework is open source
leetCode-面试题 16.06: 最小差
What are the characteristics of EDI local deployment and cloud hosting solutions?
涂鸦智能携多款重磅智能照明解决方案,亮相2022美国国际照明展
numpy.logical_and()
tf.errors
numpy. linspace()
如何在一个页面上使用多个KindEditor编辑器并将值传递到服务器端
消息队列的作用
JMeter接口测试工具基础 — Badboy工具
23. Opencv——图像拼接项目
leetCode-498: 对角线遍历
np. float32()
Difference between package type and basic type
Internet of things? Come and see Arduino on the cloud
dedecms模板文件讲解以及首页标签替换
H5网页如何在微信中自定义分享链接
uniapp实现点击拨打电话功能