PHP implementation of the website message board function, PHP Implementation message Board _php tutorial

Source: Internet
Author: User

PHP implementation of Web message board function, PHP implementation message Board


I want to achieve this is the style, can refer to the following two sites of the message board, they are the same principle of implementation

Speech Message Board Style:

NetEase Thread Style:

Principle
Need to add two main field ID and PID in the Comment table, other fields are optional, such as article ID, reply time, reply content, reply person and so on.
Where the PID is the ID of the comment that has been replied to at this moment.
As can be seen, the inside of each layer of the PID is his last layer of comments on the ID. Take a closer look at the layout above. Is it much like a multidimensional array in PHP? If you can think of it, then it's easy.
Implementation method
1. Front Desk: This is more simple div embedded Div. Then set the div's border and margin padding on the line.

                 

2, backstage: Used to two times recursion, first with recursion to the database in the reorganization of the results, after reorganization, and then recursive output above the kind of foreground code can be
Comment Table structure and contents are as follows

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

Array (  [0] = = Array   (    [id] = 1    [pid] =    [content] + comment 1   )   [1] = = Array   (    [id] = 2    [pid] = [    content] + comment 2   )   [2] = = Array   (    [id] = 3< C15/>[PID] +    [content] + comments 3   )   [3] = = Array   (    [id] + 4    [pid] = 1    [Content] + comments 4 replies. Comments 1   )   [4] = = Array   (    [id] + 5    [pid] + 1    [content] = Comments 5 replies. Comments 1   )   [5] = = Array   (    [id] + 6    [pid] = 2    [content] + comments 6 replies. 2   ) C36/>[6] = = Array   (    [id] + 7    [PID] + 4    [content] + comments 7 replies. 4   )   [7] = = Array   (    [id] = 8    [PID] + 7    [content] + comments 8 replies. 7   )   [8] = = Array   (    [id] = 9    [PID] + 8    [content] + comments 9 replies. Comments 8   )   [9] = = Array   (    [id] =    [pid] = 8
  [content] + Comments 10 replies. Comments 8   )  

And then we need to reorganize this array into the form of a message board.
Where $array is the array read above, first take out the PID default is empty, and then recursive, in the removal of the PID is the current Comment ID array

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;   }  }  

After the reorganization, you can get the following array, you can see that the style of this array is similar to the foreground comment style

Array ([0] = = Array ([id] = 1 [PID] + [content] + comment 1 [child] = = Array ([0] =         > Array ([id] = 4 [PID] + 1 [content] + comments 4 replies. 1 [Child] = Array ([0] = = Array ([id] = 7 [PID] + 4 [content] + + Comment  7 replies. Comments 4 [Child] = = Array ([0] = = Array ([id] = =                 8 [PID] + 7 [content] + comments 8 replies. Comments 7 [child] + = Array                    ([0] = = Array ([id] = 9 [pid] = 8                     [Content] + comments 9 replies. Comments 8 [child] + = Array (                    )) [1] = = Array ([id] = 10  [PID] = 8                  [Content] + comments 10 replies. Comments 8 [child] + = Array ( ))))) ()))) [1] =& Gt         Array ([id] = 5 [PID] + 1 [content] + comments 5 replies. 1 [Child] = Array ()))) [1] = = Array ([id] + 2 [pid] = [Content] + comments 2 [child] =& Gt Array ([0] = = Array ([id] = 6 [PID] + 2 [content] + comments 6 replies. 2 [ Child] = = Array ())) [2] = = Array ([ID] + 3 [pid] = [Conte  NT] + comments 3 [Child] = Array ()))

Once you get the above array, you can use the 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 this, is the reorganization of the array, and then traverse the output on the line.

The above is the PHP implementation of the message board and NetEase thread style of the whole process, we can also try to innovate, I hope this article on everyone's learning to help.

http://www.bkjia.com/PHPjc/1067828.html www.bkjia.com true http://www.bkjia.com/PHPjc/1067828.html techarticle PHP Implementation of the website message board function, PHP implementation of the message board I want to achieve is this style, can refer to the following two sites of the message board, their implementation principles are the same smooth ...

  • Related Article

    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.