Home > Backend Development > PHP Tutorial > PHP implements Changyan message board and NetEase post style

PHP implements Changyan message board and NetEase post style

墨辰丷
Release: 2023-03-30 07:08:01
Original
1698 people have browsed it

This article mainly introduces the implementation of Changyan message board and NetEase post style in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

Changing 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, reply content, reply person, 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 method1. 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. The structure and content of the comment table are as follows

Then directly read out the contents in this table All comments. 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, which is to reorganize the array, Then just loop through the output.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

The role of php anonymous functions and closures

PHP method of sorting two-dimensional arrays

How to set header information in PHP

The above is the detailed content of PHP implements Changyan message board and NetEase post style. For more information, please follow other related articles on the PHP Chinese website!

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