1.composer下载thinkphp3.2
composer create-project topthink/thinkphp your-project-name
2.创建数据库
CREATE TABLE `comments` (
`id` INT ( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT,
`content` text COLLATE utf8_unicode_ci NOT NULL,
`user_id` INT ( 11 ) UNSIGNED NOT NULL,
`parent_id` INT ( 11 ) UNSIGNED DEFAULT NULL,
`likes` INT ( 11 ) NOT NULL DEFAULT '0',
`created_at` datetime NOT NULL,
PRIMARY KEY ( `id` )
) ENGINE = INNODB AUTO_INCREMENT = 9 DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
3.连接数据库
Application\Common\Conf\
<?php
return array(
//'配置项'=>'配置值'
//数据库配置信息
'DB_TYPE' => 'mysql', // 数据库类型
'DB_HOST' => '127.0.0.1', // 服务器地址
'DB_NAME' => 'comments', // 数据库名
'DB_USER' => 'root', // 用户名
'DB_PWD' => '123456', // 密码
'DB_PORT' => 3306, // 端口
'DB_PARAMS' => array(), // 数据库连接参数
'DB_PREFIX' => '', // 数据库表前缀
'DB_CHARSET'=> 'utf8', // 字符集
'DB_DEBUG' => TRUE, // 数据库调试模式 开启后可以记录SQL日志
);
4.HTML页面
comments\Application\Home\View\Index\
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/layer/3.1.1/theme/default/layer.css">
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
form {
margin-bottom: 20px;
}
input[type="text"],
input[type="submit"] {
padding: 5px;
margin-right: 10px;
}
ul {
list-style: none;
padding: 0;
margin: 0;
}
li {
margin-bottom: 10px;
}
li p {
margin: 0;
padding: 5px;
background-color: #f5f5f5;
border: 1px solid #ddd;
border-radius: 4px;
}
li a {
display: inline-block;
margin-right: 10px;
color: #337ab7;
text-decoration: none;
}
.likes {
color: #999;
}
#reply-form {
margin-top: 10px;
}
#reply-form input[type="text"],
#reply-form input[type="submit"] {
display: block;
margin-top: 5px;
}
</style>
</head>
<body>
<!-- 评论表单 -->
<form action="{:U('Index/add')}" method="post">
<input type="text" name="content" placeholder="请输入评论内容">
<input type="submit" value="发表评论">
</form>
<!-- 评论列表 -->
<ul>
<?php foreach ($comments as $comment): ?>
<li>
<p><?php echo $comment['content']; ?></p>
<a href="#" onclick="reply(<?php echo $comment['id']; ?>)">回复</a>
<a href="#" onclick="like(<?php echo $comment['id']; ?>)">点赞</a>
<span class="likes">点赞数:<?php echo $comment['likes']; ?></span>
<?php if (!empty($comment['replies'])): ?>
<ul>
<?php foreach ($comment['replies'] as $reply): ?>
<li>
<p><?php echo $reply['content']; ?></p>
<!-- 显示更多回复 -->
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<!-- 回复表单 -->
<div id="reply-form" style="display: none;">
<form action="{:U('Index/addReply')}" method="post">
<input type="hidden" name="parent_id" id="parent-id">
<input type="text" name="content" placeholder="请输入回复内容">
<input type="submit" value="发表回复">
</form>
</div>
</body>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.staticfile.org/layer/3.1.1/layer.js"></script>
<script>
// 回复评论
function reply(parentId) {
document.getElementById('parent-id').value = parentId;
document.getElementById('reply-form').style.display = 'block';
}
// 点赞评论
function like(commentId) {
$.post("{:U('Index/like')}", {
"comment_id": commentId
}, function (data) {
if (data.status == 1) {
var likesSpan = $("#comment-" + commentId + " .likes");
likesSpan.text("点赞数:" + data.likes);
layer.msg(data.info, {
icon: 1
});
// 刷新点赞次数
location.reload();
} else {
layer.msg(data.info, {
icon: 2
});
}
}, "json");
}
</script>
</html>
5.控制器
comments\Application\Home\Controller\IndexController.class.php
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller
{
public function index()
{
$comments = M('comments')->where('parent_id IS NULL')->select();
foreach ($comments as &$comment) {
$comment['replies'] = M('comments')->where('parent_id = ' . $comment['id'])->select();
}
$this->assign('comments', $comments);
$this->display();
}
public function add()
{
$content = I('post.content');
$userId = 1; // 假设用户ID为1,根据实际情况获取用户ID
$data = [
'content' => $content,
'user_id' => $userId,
'created_at' => date('Y-m-d H:i:s'),
];
$result = M('comments')->add($data);
if ($result) {
$this->success('评论成功');
} else {
$this->error('评论失败');
}
}
public function addReply()
{
$content = I('post.content');
$parentId = I('post.parent_id');
$userId = 1; // 假设用户ID为1,根据实际情况获取用户ID
$data = [
'content' => $content,
'user_id' => $userId,
'parent_id' => $parentId,
'created_at' => date('Y-m-d H:i:s'),
];
$result = M('comments')->add($data);
if ($result) {
$this->success('回复成功');
} else {
$this->error('回复失败');
}
}
public function like()
{
$commentId = I('post.comment_id');
$result = M('comments')->where('id = ' . $commentId)->setInc('likes');
if ($result) {
$this->success('点赞成功');
} else {
$this->error('点赞失败');
}
}
}