Detailed explanation of the function of implementing website message board in PHP

*文
Release: 2023-03-19 09:46:01
Original
4033 people have browsed it

This article mainly introduces how to implement the website message board function in PHP. It is mainly modeled on the Changyan message board and NetEase post styles. Interested friends can refer to it. I hope to be helpful.

What I want to achieve is the style shown below. You can refer to the message boards of the two websites below. Their implementation principles are the same

Chang Message board style:

NetEase post style:

##PrincipleYou need to add two main fields, id and pid, to the comment table. Other fields can be added at will, such as article id, reply time, and reply. Content, responders, etc.
The pid is the ID of the comment that has been replied to currently.
As you can see from the picture above, the pid of each layer is the id of the comment on the previous layer. Take a closer look at the layout above. Is it very similar to multidimensional arrays in PHP? If you can think of it, it's easy.

Implementation method
1. Front desk: This is relatively simple, it is p embedded in p. Then set the border and margin padding of p

<p class="comment"> 
 <p class="comment"> 
  <p class="comment"> 
 
  </p> 
 </p> 
</p> 
 
<p class="comment"> 
 
</p>
Copy after login

2. Backstage: Two recursions are used. First, recursion is used to reorganize the results in the database. After reorganization, then use recursion to output the above front-end code
comment table structure and content are as follows

Then directly Read all comments in this table. You can get the following array

Array 
( 
 [0] => Array 
  ( 
   [id] => 1 
   [pid] => 
   [content] => 评论1 
  ) 
 
 [1] => Array 
  ( 
   [id] => 2 
   [pid] => 
   [content] => 评论2 
  ) 
 
 [2] => Array 
  ( 
   [id] => 3 
   [pid] => 
   [content] => 评论3 
  ) 
 
 [3] => Array 
  ( 
   [id] => 4 
   [pid] => 1 
   [content] => 评论4回复评论1 
  ) 
 
 [4] => Array 
  ( 
   [id] => 5 
   [pid] => 1 
   [content] => 评论5回复评论1 
  ) 
 
 [5] => Array 
  ( 
   [id] => 6 
   [pid] => 2 
   [content] => 评论6回复评论2 
  ) 
 
 [6] => Array 
  ( 
   [id] => 7 
   [pid] => 4 
   [content] => 评论7回复评论4 
  ) 
 
 [7] => Array 
  ( 
   [id] => 8 
   [pid] => 7 
   [content] => 评论8回复评论7 
  ) 
 
 [8] => Array 
  ( 
   [id] => 9 
   [pid] => 8 
   [content] => 评论9回复评论8 
  ) 
 
 [9] => Array 
  ( 
   [id] => 10 
   [pid] => 8 
   [content] => 评论10回复评论8 
  ) 
 
)
Copy after login

Then we need to reorganize this array into the message board form above

where $array is the array read above, first take out the pid which is empty by default. Then recurse, after taking out the array whose pid is the current comment id

public static function tree($array,$child="child", $pid = null) 
{ 
 $temp = []; 
 foreach ($array as $v) { 
  if ($v[&#39;pid&#39;] == $pid) { 
   $v[$child] = self::tree($array,$child,$v[&#39;id&#39;]); 
   $temp[] = $v; 
  } 
 } 
 return $temp; 
}
Copy after login

After reorganization, you can get the following array. You can see that the style of this array is very similar to the front comment style

Array 
( 
 [0] => Array 
  ( 
   [id] => 1 
   [pid] => 
   [content] => 评论1 
   [child] => Array 
    ( 
     [0] => Array 
      ( 
       [id] => 4 
       [pid] => 1 
       [content] => 评论4回复评论1 
       [child] => Array 
        ( 
         [0] => Array 
          ( 
           [id] => 7 
           [pid] => 4 
           [content] => 评论7回复评论4 
           [child] => Array 
            ( 
             [0] => Array 
              ( 
               [id] => 8 
               [pid] => 7 
               [content] => 评论8回复评论7 
               [child] => Array 
                ( 
                 [0] => Array 
                  ( 
                   [id] => 9 
                   [pid] => 8 
                   [content] => 评论9回复评论8 
                   [child] => Array 
                    ( 
                    ) 
 
                  ) 
 
                 [1] => Array 
                  ( 
                   [id] => 10 
                   [pid] => 8 
                   [content] => 评论10回复评论8 
                   [child] => Array 
                    ( 
                    ) 
 
                  ) 
 
                ) 
 
              ) 
 
            ) 
 
          ) 
 
        ) 
 
      ) 
 
     [1] => Array 
      ( 
       [id] => 5 
       [pid] => 1 
       [content] => 评论5回复评论1 
       [child] => Array 
        ( 
        ) 
 
      ) 
 
    ) 
 
  ) 
 
 [1] => Array 
  ( 
   [id] => 2 
   [pid] => 
   [content] => 评论2 
   [child] => Array 
    ( 
     [0] => Array 
      ( 
       [id] => 6 
       [pid] => 2 
       [content] => 评论6回复评论2 
       [child] => Array 
        ( 
        ) 
 
      ) 
 
    ) 
 
  ) 
 
 [2] => Array 
  ( 
   [id] => 3 
   [pid] => 
   [content] => 评论3 
   [child] => Array 
    ( 
    ) 
 
  ) 
 
)
Copy after login

After getting the above array, use recursion to output it

public static function traverseArray($array) 
{ 
 foreach ($array as $v) { 
  echo "<p class=&#39;comment&#39; style=&#39;width: 100%;margin: 10px;background: #EDEFF0;padding: 20px 10px;border: 1px solid #777;&#39;>"; 
  echo $v[&#39;content&#39;]; 
  if ($v[&#39;child&#39;]) { 
   self::traverseArray($v[&#39;child&#39;]); 
  } 
  echo "</p>"; 
 
 } 
 
}
Copy after login

Then you can see


The principle is like this, it is reorganization Just download the array and then iterate through the output.

Related recommendations:

Detailed explanation of how PHP displays hexadecimal image data to the web page

Detailed explanation of ThinkPHP's behavior extensions and plug-ins

##Detailed explanation of how PHP implements a simple search box automatic prompt function

The above is the detailed content of Detailed explanation of the function of implementing website message board in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!