【发布时间】:2018-04-21 12:07:01
【问题描述】:
我正在使用 while 循环从 mysql 表中获取用户 cmets,并希望在评论 div 中给出的按钮上放置一些条件类。
每条评论都有两个按钮:
- 点赞按钮
- 拇指向下按钮
如果用户已经点赞了任何评论,我想给一个班级名称active,这样如果将来他点击相同评论的点赞,它将反转数字之类的。
我正在讨论这两个表
cmets
+----+--------+---------------+---------+
| id | userid | usercom | comname |
+----+--------+---------------+---------+
| 35 | 5 | check comment | 12 |
| 36 | 6 | comment test | 12 |
| 37 | 6 | third comment | 12 |
| 38 | 5 | number four | 12 |
| 39 | 7 | fifth | 13 |
| 40 | 4 | 6th | 13 |
| 41 | 18 | seven | 13 |
+----+--------+---------------+---------+
喜欢或不喜欢
+----+-------+------+-------+
| id | vtype | uid | comid |
+----+-------+------+-------+
| 1 | 0 | 5 | 35 |
| 2 | 1 | 6 | 35 |
| 3 | 1 | 7 | 35 |
| 4 | 0 | 8 | 36 |
| 5 | 1 | 5 | 36 |
| 6 | 1 | 9 | 35 |
| 7 | 1 | 10 | 36 |
| 8 | 1 | 11 | 36 |
| 9 | 1 | 20 | 35 |
| 10 | 0 | 9 | 35 |
| 11 | 1 | 21 | 37 |
+----+-------+------+-------+
我正在从这些表中获取数据并在此处回显...
.vbtn {
display: inline-block;
margin-bottom: 0;
font-size: 13px;
font-weight: 300;
white-space: nowrap;
vertical-align: middle;
-ms-touch-action: manipulation;
touch-action: manipulation;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background: none!important;
color: inherit;
color: #06C;
border: none;
padding: 0!important;
font: inherit;
}
.like.active span {
display: none;
}
.like.active:before {
font-family: FontAwesome;
content: "\f164";
color: #009900;
}
.dislike.active span {
display: none;
}
.dislike.active:before {
font-family: FontAwesome;
content: "\f165";
color: #FF0000;
}
<?php
// ...
$sid = "18"; //session id
$c_name = "12"; //article-post id
$sel = "SELECT * FROM `likesordislikes` where `uid` = :sid AND `comid` = :comment";
if (isset($_POST['likes'])) {
$comid = mysqli_real_escape_string($con, $_POST['comid']);
try {
$stmt = $DB->prepare($sel);
$stmt->bindValue(":sid", $sid);
$stmt->bindValue(":comment", $comid);
$stmt->execute();
$fetch = $stmt->fetchAll();
if (count($fetch) == 0) {
$insert = "INSERT INTO `likesordislikes` (`vtype` , `uid` , `comid`) VALUES " . "( :like, :likerid , :comment)";
$stmt = $DB->prepare($insert);
$stmt->bindValue(":like", "1");
$stmt->bindValue(":likerid", $sid);
$stmt->bindValue(":comment", $comid);
$stmt->execute();
} elseif (count($fetch) > 0) {
if ($fetch[0]["vtype"] == "1") {
$delete = "DELETE FROM `likesordislikes` where `uid` = :sid AND `comid` = :commnt";
$stmt = $DB->prepare($delete);
$stmt->bindValue(":sid", $sid);
$stmt->bindValue(":commnt", $comid);
$stmt->execute();
} elseif ($fetch[0]["vtype"] == "0") {
$sql = "UPDATE `likesordislikes` SET `vtype` = '1' WHERE `uid` = :id AND `comid` = :commn";
$stmt = $DB->prepare($sql);
$stmt->bindValue(":id", $sid);
$stmt->bindValue(":commn", $comid);
$stmt->execute();
}
}
}
catch (Exception $ex) {
echo $ex->getMessage();
}
}
if (isset($_POST['dislikes'])) {
$comid = mysqli_real_escape_string($con, $_POST['comid']);
try {
$stmt = $DB->prepare($sel);
$stmt->bindValue(":sid", $sid);
$stmt->bindValue(":comment", $comid);
$stmt->execute();
$fetch = $stmt->fetchAll();
if (count($fetch) == 0) {
$insert = "INSERT INTO `likesordislikes` (`vtype` , `uid` , `comid`) VALUES " . "( :unlke, :likerid , :comment);";
$stmt = $DB->prepare($insert);
$stmt->bindValue(":unlke", "0");
$stmt->bindValue(":likerid", $sid);
$stmt->bindValue(":comment", $comid);
$stmt->execute();
} else {
if ($fetch[0]["vtype"] == "0") {
$delete = "DELETE FROM `likesordislikes` where uid = :sid AND comid = :commnt";
$stmt = $DB->prepare($delete);
$stmt->bindValue(":sid", $sid);
$stmt->bindValue(":commnt", $comid);
$stmt->execute();
} elseif ($fetch[0]["vtype"] == "1") {
$sql = "UPDATE `likesordislikes` SET `vtype` = '0' WHERE `uid` = :id AND comid = :commn";
$stmt = $DB->prepare($sql);
$stmt->bindValue(":id", $sid);
$stmt->bindValue(":commn", $comid);
$stmt->execute();
}
}
}
catch (Exception $ex) {
echo $ex->getMessage();
}
}
$slt = "SELECT" .
" c.*," .
" SUM(CASE WHEN lod.vtype=1 THEN 1 ELSE 0 END) likes," .
" SUM(CASE WHEN lod.vtype=0 THEN 1 ELSE 0 END) dislikes" .
" FROM" .
" comments c LEFT JOIN likesordislikes lod ON lod.comid=c.id" .
" WHERE" .
" c.comname = '$c_name' AND c.media = '$type'" .
" GROUP BY" .
" c.id";
$res = mysqli_query($con, $slt);
while($fetc = mysqli_fetch_array($res)) {
//$select3 = "SELECT c.* FROM comments c LEFT JOIN likesordislikes lod ON c.id=lod.comid WHERE `uid` = :sid"; (It was my question code)
$select3 = "SELECT vtype FROM likesordislikes WHERE comid = :commentid AND `uid` = :sid LIMIT 1"; // its the solution i got in answer
try {
$stmtt = $DB->prepare($select3);
$stmtt->bindValue(":commentid", $fetc['id']); //update from the answer
$stmtt->bindValue(":sid", $sid);
$stmtt->execute();
$vfetch = $stmtt->fetchAll();
if (count($vfetch) == 0) {
$likeclass = "";
$dislikeclass = "";
}
elseif (count($vfetch) > 0) {
if ($vfetch[0]["vtype"] == "1") {
$likeclass = "active";
$dislikeclass = "";
}
elseif ($vfetch[0]["vtype"] == "0") {
$likeclass = "";
$dislikeclass = "active";
}
}
}
catch (Exception $ex) {
echo $ex->getMessage();
}
?>
<div class="container1">
<div class="div-right-mid">
<span class="comment-text">
<?php echo $fetc['usercom'] ?>
</span>
</div>
<div id="rating-votes">
<button class="vbtn like <?php echo $likeclass; ?>" value="<?php echo $comid ?>">
<span>
<i class="fa fa-thumbs-o-up" aria-hidden="true">
</i>
</span>
</button>
<span class="likes">
<font color="#6d7371">
<?php echo $fetc['likes']; ?>
</font>
</span>
<button class="vbtn dislike <?php echo $dislikeclass; ?>" value="<?php echo $comid ?>">
<span>
<i class="fa fa-thumbs-o-down" aria-hidden="true">
</i>
</span>
</button>
<span class="dislikes">
<font color="#6d7371">
<?php echo $fetc['dislikes']; ?>
</font>
</span>
</div>
</div>
<?php
}
?>
更新js
<script type="text/javascript">
$('.rating-votes').likeDislike({
reverseMode: true,
click: function (value, l, d, event) {
var likes = $(this.element).find('.likes');
var dislikes = $(this.element).find('.dislikes');
var comid = $(".vbtn").val();
var data = {comid:comid};
if(value === 1){
data.likes = true;
}else{
data.dislikes = true;
}
$.ajax({
url: '',
type: 'POST',
data: data,
success: function (data) {
likes.text(parseInt(likes.text()) + l);
dislikes.text(parseInt(dislikes.text()) + d);
}
});
}
});
</script>
<script src="like-dislike.js"></script>
这个 php 代码没有给我所需的结果。
我面临三个问题...
当用户点赞评论时,点赞按钮未显示活动类(已解决)
-
只有第 1 条评论的拇指向上/拇指向下在点击时起作用,第 2 条第 3 次和所有剩余评论的按钮在点击时不起作用(已解决)
李> Ajax 调用未向 php 发送数据(已编辑)
CSS 显示登录用户的任何特定评论的活动类的拇指向上/向下拇指按钮会发生什么
【问题讨论】:
-
fetchAll返回一个包含所有结果集行的数组。根据$likeclass值更改aria-hidden属性值的代码在哪里? -
@camelsWriteInCamelCase 我正在通过更新提供有问题的 js 代码。
-
你有多个
<div id="rating-votes">元素吗? -
@Piyin 是的先生,因为它在while循环中,如果数据库中有5个cmets,那么将创建5个具有相同id的div,包括其中的每个评论。 抱歉 有问题,while 循环在 cmets div 开始之前关闭。现在编辑问题并在 cmets div 结束后关闭 while 循环。
-
@JavedKhan 好的,那么,这是另一个问题。我更新了我的答案以帮助您解决
2和3
标签: javascript php jquery ajax