php實現網站留言板功能_php執行個體

來源:互聯網
上載者:User

我要實現的就是下圖的這種樣式,可參考下面這兩個網站的留言板,他們的實現原理都是一樣的

暢言留言板樣式:

網易跟帖樣式:

原理
需要在評論表添加兩個主要欄位 id 和 pid  ,其他欄位隨意添加,比如文章id、回複時間、回複內容、回複人什麼的。
其中pid就是當前已經回複過的評論的id。
從上圖可以看出,裡面每一層的pid就是就是他上一層評論的id。仔細觀察下上面的布局。是不是很像PHP中的多維陣列?如果你能想到,那麼就簡單了。
實現方法
1.前台:這個比較簡單 就是div嵌div。然後設定div的border和margin  padding就行了

<div class="comment">  <div class="comment">   <div class="comment">    </div>  </div> </div>  <div class="comment">  </div> 

2、後台 :用到了兩次遞迴,首先用遞迴把資料庫中的結果重組下,重組之後,然後用遞迴輸出上面的那種前台代碼即可
comment表結構和內容如下

然後直接讀出這個表中的所有評論。可以得到如下數組

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   )  ) 

然後我們就需要把這個數組重組成上面的那種留言板形式的
其中$array就是上面讀取出來的數組,首先取出pid預設為空白的,然後遞迴,在取出pid為當前評論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; } 

重組後,可以得到下面的這個數組,可以看到,這個數組的樣式已經和前台評論樣式很像了

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     (     )    )  ) 

得到上面的數組後 ,再用遞迴輸出即可

public static function traverseArray($array) {  foreach ($array as $v) {   echo "<div 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 "</div>";   }  } 

然後即可看到

原理就是這樣,就是重組下數組,然後遍曆輸出就行了。

以上就是php實現暢言留言板和網易跟帖樣式的全部過程,大家也可以嘗試進行創新,希望這篇文章對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.