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!