PHP Implementation of Speech message board and NetEase thread style

Source: Internet
Author: User
This article mainly introduces the PHP implementation of the word message board and NetEase thread style, interested in the reference of friends, I hope to help you.

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 relatively simple P-embedding p. Then set P's border and margin padding on the line.

<p class= "comment" >  <p class= "comment" >   <p class= "comment" >    </p>  </p > </p>  <p class= "comment" >  </p>

2, backstage: Used to two times recursion, first with recursion to the results of the reorganization of the database, after reorganization, and then recursive output above the kind of foreground code can be comment table structure and content as follows

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

array ([0] = = Array ([id] + 1 [pid] = [Content] + comments 1    ) [1] = = Array ([id] = 2 [pid] = [content] + comment 2) [2] = = Array ([ID] + 3    [PID] = [content] + comment 3) [3] = = Array ([id] + 4 [PID] + 1 [content] + comments 4 replies. 1 ) [4] = = Array ([id] = 5 [PID] = 1 [content] + comments 5 replies. 1) [5] = = Array ([ID] + 6 [PID] + 2 [content] + comments 6 replies. Comments 2) [6] = = Array ([id] = 7 [PID] + 4 [conten T] + comments 7 replies. Comments 4) [7] = = Array ([id] + 8 [pid] = 7 [content] + comments 8 replies. 7) [8] = A Rray ([id] = 9 [PID] + 8 [content] + comments 9 replies. 8) [9] = = Array ([id] = [PID] = 8 [Content] + comments 10 replies. 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;   }  }  return $temp; }

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 "<p class= ' comment ' style= ' width: 100%;margin:10px;background: #EDEFF0;p adding:20px 10px;border:1px solid #777; ' > ";   echo $v [' content '];   if ($v [' child ']) {    self::traversearray ($v [' child ']);   }   echo "</p>";   }  }

Then you can see

The principle is this, is the reorganization of the array, and then traverse the output on the line.

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.