PHP實現暢言留言板和網易跟帖樣式


原文:http://justcoding.iteye.com/blog/2251192
 
我要實現的就是下圖的這種樣式,可參考下面這兩個網站的留言板,他們的實現原理都是一樣的
http://changyan.sohu.com/blog/?p=177
http://www.reddit.com/r/wow/comments/2y6739/method_vs_blackhand_mythic_kill_video_world_1st/
轉:php實現暢言留言板和網易跟帖樣式 - wu - x86 PC
 和
轉:php實現暢言留言板和網易跟帖樣式 - wu - x86 PC
 
原理
需要在評論表添加兩個主要字段 id 和 pid  ,其他字段隨意添加,比如文章id、回復時間、回復內容、回復人什么的。
其中pid就是當前已經回復過的評論的id。
從上圖可以看出,里面每一層的pid就是就是他上一層評論的id。仔細觀察下上面的布局。是不是很像PHP中的多維數組?如果你能想到,那么就簡單了。
 
實現方法
1.前台 這個比較簡單 就是div嵌div。然后設置div的border和margin  padding就行了
Html代碼
<div class="comment">  
    <div class="comment">  
        <div class="comment">  
   
        </div>  
    </div>  
</div>  
   
<div class="comment">  
   
</div>  
 
2、后台   用到了兩次遞歸,首先用遞歸把數據庫中的結果重組下,重組之后,然后用遞歸輸出上面的那種前台代碼即可
comment表結構和內容如下
php實現暢言留言板和網易跟帖樣式
php實現暢言留言板和網易跟帖樣式
然后直接讀出這個表中的所有評論。可以得到如下數組
Php代碼
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的數組
Php代碼
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;  
}  
 
重組后,可以得到下面的這個數組,可以看到,這個數組的樣式已經和前台評論樣式很像了。
Php代碼
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  
                (  
                )  
   
        )  
   
)  
 
得到上面的數組后 ,再用遞歸輸出即可。
Php代碼
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實現暢言留言板和網易跟帖樣式 - wu - x86 PC
 
原理就是這樣 ,就是重組下數組,然后遍歷輸出就行了。
 
原文:http://www.cnsecer.com/7211.html
轉自:php實現暢言留言板和網易跟帖樣式

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM