gpt4 book ai didi

php - Yii2 简单的喜欢不喜欢按钮保存在数据库中

转载 作者:行者123 更新时间:2023-11-29 10:53:37 25 4
gpt4 key购买 nike

我在这项任务上挣扎了近两天!我看了很多帖子和视频,但不知道该怎么做。到目前为止,我所在的地方是一张带有 like_id | 的表格。用户 ID | post_id 列,like_id 是主键,user_id 是喜欢该评论的用户的外键,post_id 是与此评论相关的帖子的外键。我认为我可以阻止用户多次点赞。

我的操作评论是:

public function actionComments($id)
{
$comments = \Yii::$app->db->createCommand("SELECT *
FROM post_comments
WHERE post_id=$id
ORDER BY comment_id
DESC ")->queryAll();

$likes = \Yii::$app->db->createCommand("SELECT *
FROM likes")->queryAll();

if(\Yii::$app->user->isGuest)
{
return $this->redirect(['../site/error', '405', 'You have to be loged in, to see this content!']);
}

return $this->render('comments',[
'comments' => $comments,
'likes' => $likes
]);
}

我的评论 View :

<?php

use yii\helpers\Html;
use app\models\BlogUser;
use app\controllers\PostController;
?>
<div class="container">
<div class="row">
<div class="col-md-12">
<?php foreach ($comments as $comment): ?>
<div class='col-md-3 post-prof-img'>
<?php
$currUser = BlogUser::find()->where(['id' => $comment['author_id']])->one();
?>
<?= Html::img('../images/' . $currUser->image, ['class' => 'tall img-circle']) ?>
<p class="text-center title-par">
<em><strong><?= $currUser->username ?></strong></em>
</p>
</div>
<div class='col-md-9 col-md-offset-1'>
<div class='post-comment'>
<p><em><?= $comment['comment_content'] ?></em></p>
</div>
<div class='comment-options'>
<div class='col-md-8'>
<?php
if(\Yii::$app->user->identity->id == $comment['author_id'] || PostController::isAdmin())
{
echo Html::a('Edit',['update-comment', 'id' => $comment['comment_id']]);
echo Html::a('Delete',
['delete-comment', 'id' => $comment['comment_id']],
['data' => ['method' => 'POST']]);
}
echo Html::a('Like');
echo Html::a('Dislike');
?>
</div>
<div class='col-md-4 text-right'>
<div class="ajax-helper">
<span class='glyphicon glyphicon-hand-up' style="color:green"></span>
<!--LIKES COUNT-->
<span class='glyphicon glyphicon-hand-down' style="color:red"></span>
<!--DISLIKES COUNT -->
</div>
</div>
</div>
</div>
<?php endforeach; ?>
<div>
<?= Html::a('Add Comment', ['create-comment', 'id' => $_GET['id']],['class' => 'btn btn-primary add-comment', 'style'=>'margin-top: 2%']) ?>
</div>
</div>
</div>
</div>

我知道在这种情况下我必须使用ajax!我一团糟!我请求一些建议。不是完整的答案,只是建议!预先感谢您!

最佳答案

使用pjax进行动态输出。编写您的代码来选择是否显示喜欢/不喜欢按钮。您可能需要一个包含 PK、post_id、user_id 的表,例如

Controller /操作:

public function actionTestPjax1($post_id = null, $like = null) {
// try get like per post
if (!empty($post_id) && !Yii::$app->user->isGuest) {
$data = Like::find()->where(['user_id' => Yii::$app->user->id, 'post_id' => $post_id])->one();

if (!empty($like)) {
// set from like /dislike
if (empty($data)) {
// new record
$data = new Like;
$data->post_id = $post_id;
}
$data->like = $like;
$data->save();
} else {
// we want to get the current state
if (!empty($data)) {
$like = $data->like;
}
}
} else {
// error
}
return $this->render('test-pjax1', ['like' => $like, 'time' => time()]);
}

查看测试-pjax1:

<?php
use \yii\widgets\Pjax;
use yii\helpers\Html;
?>

timestamp <code><?= $time ?></code>
<hr>

<?php Pjax::begin([
'id' => 'pjax1',
'enablePushState' => false,
'enableReplaceState' => false,
'linkSelector' => '#pjax1 a',
'timeout' => 10000,
]) ?>

timestamp <code><?= $time ?></code> (in pjax container)

<?php if (empty($like)) { ?>

<hr>
<?= Html::a('LIKE', [site/test-pjax1', 'like' => 1, ]) ?>
/
<?= Html::a('DISLIKE', ['site/test-pjax1', 'like' => -1, ]) ?>

<?php } else { ?>

<hr>
<p>
<?= ($like == 1) ? 'you like me :)' : 'you do not like me :('; ?>
</p>

<?php } ?>


<?php Pjax::end() ?>

关于php - Yii2 简单的喜欢不喜欢按钮保存在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43346935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com