Yii2简单就像保存在数据库中的不喜欢按钮
I am struggling from almost two days with this task! I took a look at a lot of post and videos but didn't realize how to do it. Where i became so far is a table with like_id | user_id | post_id column
, like_id is primary key, user_id is foreign key for the user who liked the comment and post_id is foreign key for the post for who is relate this comment. Whit those i think i can prevent user from multiple likes.
My actionComments is:
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
]);
}
my comments 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>
I know i have to use ajax in this case! I am in a total mess! I am begging for some advice. Not a complete answer but just advice! Thank you in advance!
use pjax for dynamic output. write your code in action to choose between show like/dislike buttons or not. you will probably need a table with PK, post_id, user_id, like
controller / action:
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()]);
}
view test-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() ?>