Home >Backend Development >PHP Tutorial >Imitation of NetEase comment building PHP+Mysql implementation
This article mainly introduces the PHP Mysql implementation of imitating NetEase comments and building a building. It has a certain reference value. Now I share it with you. Friends in need can refer to it.
You may have read NetEase comments. That kind of building-like citation, this article uses php and mysql to achieve this effect.
First design a data table to store the comment data:
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;
The key field is pid, which saves the id of the referenced comment. If there is no reference, set it to 0.
The specific implementation mainly uses a recursive algorithm to find all referenced comments (similar to the unlimited classification menu). The code is as follows:
<?php require_once"sqlHelper.class.php"; //连接数据库 $sqlHelper= new SqlHelper('localhost','root', 'root','test', 3306); //插入评论数据 if(isset($_POST) && !empty($_POST)){ $comment= array(); $comment['username'] =$_POST['name']?$_POST['name']:'匿名'; $comment['site'] =$_POST['site']?$_POST['site']:' '; $comment['pid'] =$_POST['commentid']?$_POST['commentid']:'0'; if($comment['pid'] == $comment['id']){ $comment['pid'] ='0'; } $comment['content'] =$_POST['content']?$_POST['content']:'...'; $comment['articleid'] =$_POST['articleid']?$_POST['articleid']:'1'; $comment['time'] = date ( 'Y-m-d H:i:s', 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.= '