首頁  >  文章  >  後端開發  >  仿網易評論蓋樓PHP+Mysql實現

仿網易評論蓋樓PHP+Mysql實現

不言
不言原創
2018-06-04 09:59:244093瀏覽

這篇文章主要介紹了關於仿網易評論蓋樓PHP Mysql實現,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

大家可能都看過網易評論的那種蓋樓式的引用,這邊文章就用php和mysql來實現這個效果。

先設計資料表來存放評論資料:

DROP TABLE IF EXISTS `comment`;
CREATE TABLE `comment` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(32) NOT NULL,
  `site` varchar(128) DEFAULT NULL,
  `content` varchar(1000) NOT NULL,
  `time` datetime NOT NULL,
  `pid` int(10) NOT NULL,
  `articleid` int(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

其中的關鍵欄位就是pid,儲存的是引用評論的id,如果沒有引用那麼設為0即可。

具體的實作主要是透過遞歸演算法來找出所有引用的評論(和無限分類選單類似),程式碼如下:

<?php 
    require_once"sqlHelper.class.php";
     
    //连接数据库
    $sqlHelper= new SqlHelper(&#39;localhost&#39;,&#39;root&#39;, &#39;root&#39;,&#39;test&#39;, 3306);
 
    //插入评论数据
    if(isset($_POST) && !empty($_POST)){
        $comment= array();
        $comment[&#39;username&#39;] =$_POST[&#39;name&#39;]?$_POST[&#39;name&#39;]:&#39;匿名&#39;;
        $comment[&#39;site&#39;] =$_POST[&#39;site&#39;]?$_POST[&#39;site&#39;]:&#39;
 &#39;; 
        $comment[&#39;pid&#39;] =$_POST[&#39;commentid&#39;]?$_POST[&#39;commentid&#39;]:&#39;0&#39;;
        if($comment[&#39;pid&#39;]
 == $comment[&#39;id&#39;]){
            $comment[&#39;pid&#39;] =&#39;0&#39;;
        }
        $comment[&#39;content&#39;] =$_POST[&#39;content&#39;]?$_POST[&#39;content&#39;]:&#39;...&#39;;
        $comment[&#39;articleid&#39;] =$_POST[&#39;articleid&#39;]?$_POST[&#39;articleid&#39;]:&#39;1&#39;;
        $comment[&#39;time&#39;] = date ( &#39;Y-m-d H:i:s&#39;, time () );
 
        $sql_str= $sqlHelper->get_replace_db_sql("comment",$comment);
        //echo $sql_str;
        $sqlHelper->insert_db($sql_str);
    }
?>
 
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xml:lang="en">
 
<head>
 
    <meta http-equiv="Content-Type"content="text/html;charset=UTF-8"/>
    <title>文章评论的无限引用实现</title>
    <style type="text/css">
        *{margin:0;padding:0;}
        body{margin:10px;font-size:14px;font-family:宋体}
        h1{font-size:26px;margin:10px 0 15px;}
        #commentHolder{width:540px;border-bottom:1px solid #aaa;}
        .comment{padding:5px 8px;background:#f8fcff;border:1px solid #aaa;font-size:14px;border-bottom:none;}
        .comment p{padding:5px 0;}
        .comment p.title{color:#1f3a87;font-size:12px;}
        .comment p span{float:right;color:#666}
        .comment p{background:#ffe;padding:3px;border:1px solid #aaa;line-height:140%;margin-bottom:5px;}
        .comment p span{color:#1f3a87;font-size:12px;}
    </style>
</head>
<body>
 
<?php
     
    functionp($str){
        echo"<pre class="brush:php;toolbar:false">";
        if(is_array($str))
            print_r($str);
        else
            echo$str;
        echo"
"; } /* 获取文章id对应的所有评论,以数组的形式返回结果 */ functiongetCommentByArticleId($articleid=1){ $result= array(); if(empty($articleid) || !is_numeric($articleid)){ return$result; } $sql= 'select * from comment where articleid = '. $articleid; global$sqlHelper; $result= $sqlHelper->get_all($sql); //p($result); return$result; } /* 把评论数组数据转换为html格式 */ functioncommentArr2Html($comment_arr) { $str= ''; if(is_array($comment_arr) && !empty($comment_arr)){ $str.= '

'; foreach($comment_arr as $key => $value) { $str.= '

'; $str.= '

'.$value['username'] .''; $str.= '' . $value['time'] .' 发表'; $str.= '

'; global$temp_arr; $temp_arr= array(); //这里去查找当前评论下的所有引用的评论,并格式化为html字符串 $tmpStr= ''; addCommentNode($comment_arr,$value); krsort($temp_arr);//根据key倒叙排序数组 $tmpStr= getChildComment($temp_arr);//添加所有的引用评论 $str.= $tmpStr; $str.= "

" . $value['content'] ."

"; $str.= '

'; } $str.='

'; } return$str; } /* 把temp_arr数组中保存的引用评论信息转换为html形式 */ functiongetChildComment($temp_arr){ $htmlStr= ''; if(!is_array($temp_arr) || empty($temp_arr)){ return''; } foreach($temp_arras $value){ $tmp= '

'; $tmp.= $htmlStr; $tmp.= '' . $value['username'] .' 原贴:
' . $value['content']; $tmp.= '

'; $htmlStr= $tmp; } return$htmlStr; } /* list代表某一文章下的全部评论列表 cmt代表当前要显示的评论 */ functionaddCommentNode($list,$cmt){ if(isset($cmt['pid']) && $cmt['pid'] !='0'){ $find= findParentCmt($list,$cmt['pid']);//找寻id等于当前评论的pid的评论,返回数组。 // 递归调用,只要pid不为零,就加入到引用评论列表 addCommentNode($list,$find); }else{ return; } } /** * 查找list中找寻id等于pid的数组项,并返回 * @param [type] $list [description] * @param [type] $cmtpid [description] * @return [type] [description] */ functionfindParentCmt($list,$cmtpid){ foreach($list as $key => $value) { if($value['id'] == $cmtpid){ /* 用数组的方式来保存所有引用的评论 */ global$temp_arr; $temp_arr[] =$list[$key]; //p($list[$key]);echo "
"; return$list[$key]; } } returnfalse; } $temp_arr= array();//设一个全局的数组变量来保存引用评论的信息 $list= getCommentByArticleId();//通过文章id获取所有的文章评论 $htmlStr= commentArr2Html($list);//把获取到的评论格式化转换为html形式 ?>

文章评论的无限引用PHP+Mysql实现




你的名字:
联系方式或个人网站:
选择引用的评论:
评论内容酷站网软:

相關推薦:

PHP MySQL實作模糊查詢員工資訊功能

PHP MySQL實作輸入頁碼跳到指定頁面功能範例

PHP mysql ajax實作輕量級聊天室的方法

以上是仿網易評論蓋樓PHP+Mysql實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn