Home > Article > Backend Development > Detailed explanation of the function of implementing website message board in PHP
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>
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
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 ) )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['pid'] == $pid) { $v[$child] = self::tree($array,$child,$v['id']); $temp[] = $v; } } return $temp; }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 ( ) ) )After getting the above array, use recursion to output it
public static function traverseArray($array) { foreach ($array as $v) { echo "<p class='comment' style='width: 100%;margin: 10px;background: #EDEFF0;padding: 20px 10px;border: 1px solid #777;'>"; echo $v['content']; if ($v['child']) { self::traverseArray($v['child']); } echo "</p>"; } }Then you can see
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!