Php implements website message board function _ php instance

Source: Internet
Author: User
This article describes how to implement the website message board function in php, which is based on the changyan message board and Netease post style. If you are interested, refer to the style that I want to implement, refer to the message boards of the following two websites. Their implementation principles are the same.

Changyan message board style:

Netease post style:

Principle
You need to add two main fields id and pid in the comment table. Other fields can be added at will, such as the Article id, reply time, reply content, and reply to others.
Pid is the id of the comment that has been replied.
It can be seen that the pid of each layer is the id of the previous comment. Observe the above layout carefully. Is it like a multi-dimensional array in PHP? If you can think of it, it's easy.
Implementation Method
1. Front-end: p is a simple plug-in. Set border and margin padding of p.

2. Background: Two recursion operations are used. First, the database results are reorganized using recursion. After the reorganization, the foreground code above is output recursively.
The structure and content of the comment table are as follows:

Then read all the comments in the table. The following array can be obtained:

Array ([0] => Array ([id] => 1 [pid] => [content] => comment 1) [1] => Array ([id] => 2 [pid] => [content] => comment 2) [2] => Array ([id] => 3 [pid] => [content] => comment 3) [3] => Array ([id] => 4 [pid] => 1 [content] => comment 4 reply to comment 1) [4] => Array ([id] => 5 [pid] => 1 [content] => comment 5 reply to comment 1) [5] => Array ([id] => 6 [pid] => 2 [content] => comment 6 reply to comment 2) [6] => Array ([id] => 7 [pid] => 4 [content] => comment 7 reply to comment 4) [7] => Array ([id] => 8 [pid] => 7 [content] => comment 8 reply comment 7) [8] => Array ([id] => 9 [pid] => 8 [content] => comment 9 reply to comment 8) [9] => Array ([id] => 10 [pid] => 8 [content] => Comment 10 reply comment 8 ))

Then we need to reassemble this array into the above message board form.
$ Array is the array read above. First, retrieve the default empty pid, and recursively retrieve 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 the reorganization, we can get the following array. We can see that the style of this array is already very similar to the comment style at the front end.

Array ([0] => Array ([id] => 1 [pid] => [content] => comment 1 [child] => Array ([0] => Array ([id] => 4 [pid] => 1 [content] => comment 4 reply comment 1 [child] => Array ([0] => Array ([id] => 7 [pid] => 4 [content] => comment 7 reply comment 4 [child] => Array ([0] => Array ([id] => 8 [pid] => 7 [content] => comment 8 reply comment 7 [child] => Array ([0] => Array ([id] => 9 [pid] => 8 [content] => comment 9 reply comment 8 [child] => Array ()) [1] => Array ([id] => 10 [pid] => 8 [content] => Comment 10 reply comment 8 [child] => Array ()))) )))) [1] => Array ([id] => 5 [pid] => 1 [content] => comment 5 reply comment 1 [child] => Array ()))) [1] => Array ([id] => 2 [pid] => [content] => comment 2 [child] => Array ([0] => Array ([ id] => 6 [pid] => 2 [content] => comment 6 reply comment 2 [child] => Array ()))) [2] => Array ([id] => 3 [pid] => [content] => comment 3 [child] => Array ()))

Obtain the preceding array and use recursive output.

public static function traverseArray($array) {  foreach ($array as $v) {   echo "

"; echo $v['content']; if ($v['child']) { self::traverseArray($v['child']); } echo "

"; } }

Then you can see

The principle is to reorganize the array and traverse the output.

The above is the entire process of implementing the changyan message board and Netease post style in php. You can also try to innovate. I hope this article will be helpful for your learning.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.