当前位置:网站首页>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 
                        )

                )

        )

)
原网站

版权声明
本文为[Wandering in memory of Yu Fei]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240914528968.html