- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一个简单的脚本,用户可以在不注册或登录的情况下发帖,就像忏悔网站一样,人们可以在他们的帖子上发帖并获得喜欢和不喜欢。我想像这样进行计数,然后按最受欢迎和最新的排序并将其显示在主页上,我几乎尝试了所有方法,但似乎没有任何效果,这是我的代码:
comment-like-unlike.php
?php
require_once ("db.php");
$memberId = 1;
$commentId = $_POST['comment_id'];
$likeOrUnlike = 0;
if($_POST['like_unlike'] == 1)
{
$likeOrUnlike = $_POST['like_unlike'];
}
$sql = "SELECT * FROM tbl_like_unlike WHERE comment_id=" . $commentId . " and member_id=" . $memberId;
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
if (! empty($row))
{
$query = "UPDATE tbl_like_unlike SET like_unlike = " . $likeOrUnlike . " WHERE comment_id=" . $commentId . " and member_id=" . $memberId;
} else
{
$query = "INSERT INTO tbl_like_unlike(member_id,comment_id,like_unlike) VALUES ('" . $memberId . "','" . $commentId . "','" . $likeOrUnlike . "')";
}
mysqli_query($conn, $query);
$totalLikes = "No ";
$likeQuery = "SELECT sum(like_unlike) AS likesCount FROM tbl_like_unlike WHERE comment_id=".$commentId;
$resultLikeQuery = mysqli_query($conn,$likeQuery);
$fetchLikes = mysqli_fetch_array($resultLikeQuery,MYSQLI_ASSOC);
if(isset($fetchLikes['likesCount'])) {
$totalLikes = $fetchLikes['likesCount'];
}
echo $totalLikes;
?>
评论-add.php
<?php
require_once ("db.php");
$commentId = isset($_POST['comment_id']) ? $_POST['comment_id'] : "";
$comment = isset($_POST['comment']) ? $_POST['comment'] : "";
$commentSenderName = isset($_POST['name']) ? $_POST['name'] : "";
$date = date('Y-m-d H:i:s');
$sql = "INSERT INTO tbl_comment(parent_comment_id,comment,comment_sender_name,date) VALUES ('" . $commentId . "','" . $comment . "','" . $commentSenderName . "','" . $date . "')";
$result = mysqli_query($conn, $sql);
if (! $result) {
$result = mysqli_error($conn);
}
echo $result;
?>
get-like-unlike.php
<?php
require_once ("db.php");
$commentId = $_POST['comment_id'];
$totalLikes = "No ";
$likeQuery = "SELECT sum(like_unlike) AS likesCount FROM tbl_like_unlike WHERE comment_id=".$commentId;
$resultLikeQuery = mysqli_query($conn,$likeQuery);
$fetchLikes = mysqli_fetch_array($resultLikeQuery,MYSQLI_ASSOC);
if(isset($fetchLikes['likesCount'])) {
$totalLikes = $fetchLikes['likesCount'];
}
echo $totalLikes;
?>
还有我的帖子和点赞的 index.php 代码:
<div class="comment-form-container">
<form id="frm-comment">
<div class="input-row">
<input type="hidden" name="comment_id" id="commentId"
placeholder="Name" /> <input class="input-field"
type="text" name="name" id="name" placeholder="Name" />
</div>
<div class="input-row">
<textarea class="input-field" type="text" name="comment"
id="comment" placeholder="Add a Comment"> </textarea>
</div>
<div>
<font color="white"><input type="button" class="btn-submit" id="submitButton"
value="Publish" /></font>
</div>
</form>
</div>
<div id="output"></div>
<script>
var totalLikes = 0;
var totalUnlikes = 0;
function postReply(commentId) {
$('#commentId').val(commentId);
$("#name").focus();
}
$("#submitButton").click(function () {
$("#comment-message").css('display', 'none');
var str = $("#frm-comment").serialize();
$.ajax({
url: "comment-add.php",
data: str,
type: 'post',
success: function (response)
{
var result = eval('(' + response + ')');
if (response)
{
$("#comment-message").css('display', 'inline-block');
$("#name").val("");
$("#comment").val("");
$("#commentId").val("");
listComment();
} else
{
alert("Failed to add comments !");
return false;
}
}
});
});
$(document).ready(function () {
listComment();
});
function listComment() {
$.post("comment-list.php",
function (data) {
var data = JSON.parse(data);
var comments = "";
var replies = "";
var item = "";
var parent = -1;
var results = new Array();
var list = $("<ul class='outer-comment'>");
var item = $("<li>").html(comments);
for (var i = 0; (i < data.length); i++)
{
var commentId = data[i]['comment_id'];
parent = data[i]['parent_comment_id'];
var obj = getLikesUnlikes(commentId);
if (parent == "0")
{
if(data[i]['like_unlike'] >= 1)
{
like_icon = "<img src='like.png' id='unlike_" + data[i]['comment_id'] + "' class='like-unlike' onClick='likeOrDislike(" + data[i]['comment_id'] + ",-1)' />";
like_icon += "<img style='display:none;' src='unlike.png' id='like_" + data[i]['comment_id'] + "' class='like-unlike' onClick='likeOrDislike(" + data[i]['comment_id'] + ",1)' />";
}
else
{
like_icon = "<img style='display:none;' src='like.png' id='unlike_" + data[i]['comment_id'] + "' class='like-unlike' onClick='likeOrDislike(" + data[i]['comment_id'] + ",-1)' />";
like_icon += "<img src='unlike.png' id='like_" + data[i]['comment_id'] + "' class='like-unlike' onClick='likeOrDislike(" + data[i]['comment_id'] + ",1)' />";
}
comments = "\
<div class='comment-row'>\
<div class='comment-info'>\
<span class='commet-row-label'>from</span>\
<span class='posted-by'>" + data[i]['comment_sender_name'] + "</span>\
<span class='commet-row-label'>at</span> \
<span class='posted-at'>" + data[i]['date'] + "</span>\
</div>\
<div class='comment-text'>" + data[i]['comment'] + "</div>\
<div>\
<a class='btn-reply' onClick='postReply(" + commentId + ")'>Reply</a>\
</div>\
<div class='post-action'>\ " + like_icon + " \
<span id='likes_" + commentId + "'> " + totalLikes + " likes </span>\
</div>\
</div>";
var item = $("<li>").html(comments);
list.append(item);
var reply_list = $('<ul>');
item.append(reply_list);
listReplies(commentId, data, reply_list);
}
}
$("#output").html(list);
});
}
function listReplies(commentId, data, list) {
for (var i = 0; (i < data.length); i++)
{
var obj = getLikesUnlikes(data[i].comment_id);
if (commentId == data[i].parent_comment_id)
{
if(data[i]['like_unlike'] >= 1)
{
like_icon = "<img src='like.png' id='unlike_" + data[i]['comment_id'] + "' class='like-unlike' onClick='likeOrDislike(" + data[i]['comment_id'] + ",-1)' />";
like_icon += "<img style='display:none;' src='unlike.png' id='like_" + data[i]['comment_id'] + "' class='like-unlike' onClick='likeOrDislike(" + data[i]['comment_id'] + ",1)' />";
}
else
{
like_icon = "<img style='display:none;' src='like.png' id='unlike_" + data[i]['comment_id'] + "' class='like-unlike' onClick='likeOrDislike(" + data[i]['comment_id'] + ",-1)' />";
like_icon += "<img src='unlike.png' id='like_" + data[i]['comment_id'] + "' class='like-unlike' onClick='likeOrDislike(" + data[i]['comment_id'] + ",1)' />";
}
var comments = "\
<div class='comment-row'>\
<div class='comment-info'>\
<span class='commet-row-label'>from</span>\
<span class='posted-by'>" + data[i]['comment_sender_name'] + "</span>\
<span class='commet-row-label'>at</span> \
<span class='posted-at'>" + data[i]['date'] + "</span>\
</div>\
<div class='comment-text'>" + data[i]['comment'] + "</div>\
<div>\
<a class='btn-reply' onClick='postReply(" + data[i]['comment_id'] + ")'>Reply</a>\
</div>\
<div class='post-action'> " + like_icon + " \
<span id='likes_" + data[i]['comment_id'] + "'> " + totalLikes + " likes </span>\
</div>\
</div>";
var item = $("<li>").html(comments);
var reply_list = $('<ul>');
list.append(item);
item.append(reply_list);
listReplies(data[i].comment_id, data, reply_list);
}
}
}
function getLikesUnlikes(commentId)
{
$.ajax({
type: 'POST',
async: false,
url: 'get-like-unlike.php',
data: {comment_id: commentId},
success: function (data)
{
totalLikes = data;
}
});
}
function likeOrDislike(comment_id,like_unlike)
{
$.ajax({
url: 'comment-like-unlike.php',
async: false,
type: 'post',
data: {comment_id:comment_id,like_unlike:like_unlike},
dataType: 'json',
success: function (data) {
$("#likes_"+comment_id).text(data + " likes");
if (like_unlike == 1) {
$("#like_" + comment_id).css("display", "none");
$("#unlike_" + comment_id).show();
}
if (like_unlike == -1) {
$("#unlike_" + comment_id).css("display", "none");
$("#like_" + comment_id).show();
}
},
error: function (data) {
alert("error : " + JSON.stringify(data));
}
});
}
</script>
评论列表.php
<?php
require_once ("db.php");
$memberId = 1;
$sql = "SELECT tbl_comment.*,tbl_like_unlike.like_unlike FROM tbl_comment LEFT JOIN tbl_like_unlike ON tbl_comment.comment_id = tbl_like_unlike.comment_id AND member_id = " . $memberId . " ORDER BY parent_comment_id asc, comment_id asc";
$result = mysqli_query($conn, $sql);
$record_set = array();
while ($row = mysqli_fetch_assoc($result)) {
array_push($record_set, $row);
}
mysqli_free_result($result);
mysqli_close($conn);
echo json_encode($record_set);
?>
我真的很期待一些帮助,我已经在这个脚本上工作了一段时间,这是我在启动之前需要的最后一件事,谢谢你的建议!这是一个网站的图片,只是为了看看它是如何工作的
最佳答案
您应该像这样在 comment-list.php 中编写查询
$sql = "SELECT tbl_comment.*,tbl_like_unlike.like_unlike FROM tbl_comment LEFT JOIN tbl_like_unlike ON tbl_comment.comment_id = tbl_like_unlike.comment_id AND member_id = " . $memberId . " ORDER BY tbl_like_unlike.like_unlike DESC, tbl_comment.date DESC";
这会自动对您的数据进行排序,将最喜欢的评论放在顶部,然后按最新日期排序。因此,如果一条评论与另一条评论的点赞数相同,则最新的评论将显示在顶部。
评论回复
你可以像这样在 JavaScript 中做到这一点:
for (var i = 0; i < data.length; i++) {
if(i == 0) {
//put comment at top of page
} else {
// Put comment in normal place
}
}
关于javascript - 计算相似和不同的 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53329035/
我遇到了一个让我非常难过的问题。但现在看来这一定是一个普遍的问题,我很惊讶我找不到关于它的另一个问题。 基本上,我的网站上会出现一些图标。我希望它们在每台设备上都显示出大致相同的大小——大致是可触摸的
我是一名 Java 专业人士。现在我喜欢转向其他编程语言。谁能建议我哪种编程语言更容易消化,并且该语言具有 Web 开发功能。我对 Web 开发感兴趣。 最佳答案 C# - 网络开发非常好(只需看看这
我需要为不同的 url 发送不同的响应。但是我正在使用的正则表达式不起作用。 有问题的两个正则表达式是 "/v1/users/[^/]+/permissions/domain/HTTP/" (例
让我描述一下我的问题。有一个输入字符串和一个包含数千个字符串的表。我正在寻找搜索与输入字符串最相似* 字符串的最佳方法。搜索应该返回一个包含 ~10 个建议字符串的列表,按相似度排序。字符串在数据库中
我在使用 crypt() 时遇到问题,如果用户有密码(本例中为 password1),并且他们将其更改为 password2,则散列会返回相同的结果。您可以在此处进行测试:旧链接输入 password
我问这个是因为我发现 XML 在视觉上没有吸引力。我知道 XML 非常有名,很有用,而且它被大多数(如果不是全部)程序员接受为一种非常有用的定义和结构化数据的语言;然而,我真的不觉得盯着它看、看着它或
我有工作需要在 14 天内完成。我有5名 worker 。一天正好需要3个 worker 。每个 worker 最多只能工作 9 天。每个 worker 都有自己的日期偏好,每个 worker 每天都
FileStream 或 DB 句柄类型的对象必须正确关闭,因为它在内部使用 Windows 句柄。同样,Threads 也使用 Windows 句柄吧?线程是否有一些内部机制来释放那些 Window
如果这是非常基本的,请原谅。我一直在尝试遍历行数组中的每个单元格包括标题单元格。我可以在 Nokogiri CSS 选择器中使用 OR 运算符吗? thang= Nokogiri::HTML(IO.r
默认情况下,RichTextFx(顶部控件)看起来不像 TextArea(底部控件): 我找到了一种添加圆角的方法(只需从 modena.css 复制并配置 some styles): .virtua
在 64 位 Java 中,每个对象实例往往包含一个 192-bit header , 包含 类指针, 标志和 锁(每个 64 位)。 这可能会导致小对象的大量内存开销。 尼姆的情况是否类似?用两种语
我正在开发一个具有多个功能(通过 tableview 选择)的应用程序,这些功能几乎都做同样的事情,相似的布局、相似的按钮、相似的输入字段等等。但在我相信它们自己的地方有足够的不同类(class)。每
题目地址:https://leetcode-cn.com/problems/similar-rgb-color/ 题目描述 Inthe following, every capital lette
我正在编写 R 代码,我希望它在“非调试”或“调试”模式下运行。在 Debug模式下,我希望代码打印出运行时信息。 在其他语言中,我通常会有某种打印功能,除非打开标志(用于编译或运行时),否则什么都不
我不明白这个OP接受的答案的一部分: Javascript function scoping and hoisting 作者说: " 此外,在这种情况下, function a() {} 行为与相同
我正在做一个bcmp(&v6_addr1,&v6_addr2,sizeof(v6_addr1)) (gdb) p v6_addr2 $15 = {u = {b = "0\001", '\000' ,
我有一个充满按钮的 Canvas,我将其 ID 存储在字典中。 Canvas 很长,带有垂直滚动条。有一种方法可以自动将 View 定位在给定的按钮上吗? 当使用 Text 时,txt.see(pos
我正在尝试使用 Python 2.6 和 PIL 计算两个图像的相似度(读作:Levenshtein 距离)。 我打算给我们e python-levenshtein用于快速比较的库。 主要问题: 什么
换句话说,php $object->method(); 和 $object->property = 'someValue'; 相当于,js: $object.method(); 和 $object.p
我写了一个简单的函数,它接收实现 .index() 的东西和要检查的字符列表。 我的假设是,由于字符串和元组都是不可变的,因此它们具有相似的性能(或者至少,元组的性能优于列表)。相反,元组似乎等同于列
我是一名优秀的程序员,十分优秀!