Count like and unlike php










0















I written a simple script where users can post without sign up or log in, something like conffesion website, people can post and get like and unlike on their posts. I want to count like then sort by most popular and most newest and show it on a main page, and I tried literally everything but nothing seems to work, heres my code:



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;
?>


comment-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;
?>


And my index.php code for posts and likes:



 <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 + "&nbsp;
<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 + "&nbsp;
<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>


comment-list.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);
?>


Im really looking forward for some help, ive been working on this script for a while and this is the last thing I need before launching, thanks in advice!
Here's a picture of a website, just to see how its working














share|improve this question
























  • So...what seems to be the problem? I must say, handling likes and unlikes can end up being problematic. Check out this YouTube video for a good explanation: youtube.com/watch?v=RY_2gElt3SA

    – Caleb H.
    Nov 15 '18 at 23:00











  • Likes and unlikes are working great but I dont know how to sort them by most liked

    – First Name
    Nov 15 '18 at 23:02






  • 1





    Where is your comment-list.php code? That seems like it would be the code we need to look at for sorting your comments.

    – Diddle Dot
    Nov 15 '18 at 23:02












  • I just added it, sorry for missing it

    – First Name
    Nov 15 '18 at 23:11






  • 1





    Is tbl_like_unlike.like_unlike column just an INT column keeping track of total number of likes or is there another row in tbl_like_unlike for each user that likes a comment?

    – Diddle Dot
    Nov 15 '18 at 23:16
















0















I written a simple script where users can post without sign up or log in, something like conffesion website, people can post and get like and unlike on their posts. I want to count like then sort by most popular and most newest and show it on a main page, and I tried literally everything but nothing seems to work, heres my code:



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;
?>


comment-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;
?>


And my index.php code for posts and likes:



 <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 + "&nbsp;
<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 + "&nbsp;
<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>


comment-list.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);
?>


Im really looking forward for some help, ive been working on this script for a while and this is the last thing I need before launching, thanks in advice!
Here's a picture of a website, just to see how its working














share|improve this question
























  • So...what seems to be the problem? I must say, handling likes and unlikes can end up being problematic. Check out this YouTube video for a good explanation: youtube.com/watch?v=RY_2gElt3SA

    – Caleb H.
    Nov 15 '18 at 23:00











  • Likes and unlikes are working great but I dont know how to sort them by most liked

    – First Name
    Nov 15 '18 at 23:02






  • 1





    Where is your comment-list.php code? That seems like it would be the code we need to look at for sorting your comments.

    – Diddle Dot
    Nov 15 '18 at 23:02












  • I just added it, sorry for missing it

    – First Name
    Nov 15 '18 at 23:11






  • 1





    Is tbl_like_unlike.like_unlike column just an INT column keeping track of total number of likes or is there another row in tbl_like_unlike for each user that likes a comment?

    – Diddle Dot
    Nov 15 '18 at 23:16














0












0








0








I written a simple script where users can post without sign up or log in, something like conffesion website, people can post and get like and unlike on their posts. I want to count like then sort by most popular and most newest and show it on a main page, and I tried literally everything but nothing seems to work, heres my code:



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;
?>


comment-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;
?>


And my index.php code for posts and likes:



 <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 + "&nbsp;
<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 + "&nbsp;
<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>


comment-list.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);
?>


Im really looking forward for some help, ive been working on this script for a while and this is the last thing I need before launching, thanks in advice!
Here's a picture of a website, just to see how its working














share|improve this question
















I written a simple script where users can post without sign up or log in, something like conffesion website, people can post and get like and unlike on their posts. I want to count like then sort by most popular and most newest and show it on a main page, and I tried literally everything but nothing seems to work, heres my code:



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;
?>


comment-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;
?>


And my index.php code for posts and likes:



 <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 + "&nbsp;
<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 + "&nbsp;
<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>


comment-list.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);
?>


Im really looking forward for some help, ive been working on this script for a while and this is the last thing I need before launching, thanks in advice!
Here's a picture of a website, just to see how its working











javascript php html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 23:26









Gurtej Singh

2,6261624




2,6261624










asked Nov 15 '18 at 22:55









First NameFirst Name

869




869












  • So...what seems to be the problem? I must say, handling likes and unlikes can end up being problematic. Check out this YouTube video for a good explanation: youtube.com/watch?v=RY_2gElt3SA

    – Caleb H.
    Nov 15 '18 at 23:00











  • Likes and unlikes are working great but I dont know how to sort them by most liked

    – First Name
    Nov 15 '18 at 23:02






  • 1





    Where is your comment-list.php code? That seems like it would be the code we need to look at for sorting your comments.

    – Diddle Dot
    Nov 15 '18 at 23:02












  • I just added it, sorry for missing it

    – First Name
    Nov 15 '18 at 23:11






  • 1





    Is tbl_like_unlike.like_unlike column just an INT column keeping track of total number of likes or is there another row in tbl_like_unlike for each user that likes a comment?

    – Diddle Dot
    Nov 15 '18 at 23:16


















  • So...what seems to be the problem? I must say, handling likes and unlikes can end up being problematic. Check out this YouTube video for a good explanation: youtube.com/watch?v=RY_2gElt3SA

    – Caleb H.
    Nov 15 '18 at 23:00











  • Likes and unlikes are working great but I dont know how to sort them by most liked

    – First Name
    Nov 15 '18 at 23:02






  • 1





    Where is your comment-list.php code? That seems like it would be the code we need to look at for sorting your comments.

    – Diddle Dot
    Nov 15 '18 at 23:02












  • I just added it, sorry for missing it

    – First Name
    Nov 15 '18 at 23:11






  • 1





    Is tbl_like_unlike.like_unlike column just an INT column keeping track of total number of likes or is there another row in tbl_like_unlike for each user that likes a comment?

    – Diddle Dot
    Nov 15 '18 at 23:16

















So...what seems to be the problem? I must say, handling likes and unlikes can end up being problematic. Check out this YouTube video for a good explanation: youtube.com/watch?v=RY_2gElt3SA

– Caleb H.
Nov 15 '18 at 23:00





So...what seems to be the problem? I must say, handling likes and unlikes can end up being problematic. Check out this YouTube video for a good explanation: youtube.com/watch?v=RY_2gElt3SA

– Caleb H.
Nov 15 '18 at 23:00













Likes and unlikes are working great but I dont know how to sort them by most liked

– First Name
Nov 15 '18 at 23:02





Likes and unlikes are working great but I dont know how to sort them by most liked

– First Name
Nov 15 '18 at 23:02




1




1





Where is your comment-list.php code? That seems like it would be the code we need to look at for sorting your comments.

– Diddle Dot
Nov 15 '18 at 23:02






Where is your comment-list.php code? That seems like it would be the code we need to look at for sorting your comments.

– Diddle Dot
Nov 15 '18 at 23:02














I just added it, sorry for missing it

– First Name
Nov 15 '18 at 23:11





I just added it, sorry for missing it

– First Name
Nov 15 '18 at 23:11




1




1





Is tbl_like_unlike.like_unlike column just an INT column keeping track of total number of likes or is there another row in tbl_like_unlike for each user that likes a comment?

– Diddle Dot
Nov 15 '18 at 23:16






Is tbl_like_unlike.like_unlike column just an INT column keeping track of total number of likes or is there another row in tbl_like_unlike for each user that likes a comment?

– Diddle Dot
Nov 15 '18 at 23:16













1 Answer
1






active

oldest

votes


















1














You should write your query in comment-list.php like this



$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";


This will automatically sort your data to have comment with most likes on top and then sort by the newest date. So if a comment has the same amount of likes of another comment, the newest will show up on top.



COMMENT RESPONSE



You can do it in javascript like this:



for (var i = 0; i < data.length; i++) 
if(i == 0)
//put comment at top of page
else
// Put comment in normal place







share|improve this answer

























  • Its working! How can I now show most liked post in a one section of website(just one top liked post)? I want most liked post to be displayed in a most liked section I already created, is something like that possible? btw thanks a lot, will flag as asnwer

    – First Name
    Nov 15 '18 at 23:42







  • 1





    You can do it either server side or client side. You can just take the first item in your results in javascript and display it at the top of the page since you know it will be the most liked and most recent comment, or you can sort it on the server and send the first row by itself to the client. I'll edit my answer with an example.

    – Diddle Dot
    Nov 15 '18 at 23:50












  • Can you help me with it? im never worked with like and unlike and its pushing my nerves, do you need some code?

    – First Name
    Nov 16 '18 at 0:01











  • It will be similar to how you're listing out the comments in JavaScript already.

    – Diddle Dot
    Nov 16 '18 at 0:24










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329035%2fcount-like-and-unlike-php%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You should write your query in comment-list.php like this



$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";


This will automatically sort your data to have comment with most likes on top and then sort by the newest date. So if a comment has the same amount of likes of another comment, the newest will show up on top.



COMMENT RESPONSE



You can do it in javascript like this:



for (var i = 0; i < data.length; i++) 
if(i == 0)
//put comment at top of page
else
// Put comment in normal place







share|improve this answer

























  • Its working! How can I now show most liked post in a one section of website(just one top liked post)? I want most liked post to be displayed in a most liked section I already created, is something like that possible? btw thanks a lot, will flag as asnwer

    – First Name
    Nov 15 '18 at 23:42







  • 1





    You can do it either server side or client side. You can just take the first item in your results in javascript and display it at the top of the page since you know it will be the most liked and most recent comment, or you can sort it on the server and send the first row by itself to the client. I'll edit my answer with an example.

    – Diddle Dot
    Nov 15 '18 at 23:50












  • Can you help me with it? im never worked with like and unlike and its pushing my nerves, do you need some code?

    – First Name
    Nov 16 '18 at 0:01











  • It will be similar to how you're listing out the comments in JavaScript already.

    – Diddle Dot
    Nov 16 '18 at 0:24















1














You should write your query in comment-list.php like this



$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";


This will automatically sort your data to have comment with most likes on top and then sort by the newest date. So if a comment has the same amount of likes of another comment, the newest will show up on top.



COMMENT RESPONSE



You can do it in javascript like this:



for (var i = 0; i < data.length; i++) 
if(i == 0)
//put comment at top of page
else
// Put comment in normal place







share|improve this answer

























  • Its working! How can I now show most liked post in a one section of website(just one top liked post)? I want most liked post to be displayed in a most liked section I already created, is something like that possible? btw thanks a lot, will flag as asnwer

    – First Name
    Nov 15 '18 at 23:42







  • 1





    You can do it either server side or client side. You can just take the first item in your results in javascript and display it at the top of the page since you know it will be the most liked and most recent comment, or you can sort it on the server and send the first row by itself to the client. I'll edit my answer with an example.

    – Diddle Dot
    Nov 15 '18 at 23:50












  • Can you help me with it? im never worked with like and unlike and its pushing my nerves, do you need some code?

    – First Name
    Nov 16 '18 at 0:01











  • It will be similar to how you're listing out the comments in JavaScript already.

    – Diddle Dot
    Nov 16 '18 at 0:24













1












1








1







You should write your query in comment-list.php like this



$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";


This will automatically sort your data to have comment with most likes on top and then sort by the newest date. So if a comment has the same amount of likes of another comment, the newest will show up on top.



COMMENT RESPONSE



You can do it in javascript like this:



for (var i = 0; i < data.length; i++) 
if(i == 0)
//put comment at top of page
else
// Put comment in normal place







share|improve this answer















You should write your query in comment-list.php like this



$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";


This will automatically sort your data to have comment with most likes on top and then sort by the newest date. So if a comment has the same amount of likes of another comment, the newest will show up on top.



COMMENT RESPONSE



You can do it in javascript like this:



for (var i = 0; i < data.length; i++) 
if(i == 0)
//put comment at top of page
else
// Put comment in normal place








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 23:52

























answered Nov 15 '18 at 23:35









Diddle DotDiddle Dot

596412




596412












  • Its working! How can I now show most liked post in a one section of website(just one top liked post)? I want most liked post to be displayed in a most liked section I already created, is something like that possible? btw thanks a lot, will flag as asnwer

    – First Name
    Nov 15 '18 at 23:42







  • 1





    You can do it either server side or client side. You can just take the first item in your results in javascript and display it at the top of the page since you know it will be the most liked and most recent comment, or you can sort it on the server and send the first row by itself to the client. I'll edit my answer with an example.

    – Diddle Dot
    Nov 15 '18 at 23:50












  • Can you help me with it? im never worked with like and unlike and its pushing my nerves, do you need some code?

    – First Name
    Nov 16 '18 at 0:01











  • It will be similar to how you're listing out the comments in JavaScript already.

    – Diddle Dot
    Nov 16 '18 at 0:24

















  • Its working! How can I now show most liked post in a one section of website(just one top liked post)? I want most liked post to be displayed in a most liked section I already created, is something like that possible? btw thanks a lot, will flag as asnwer

    – First Name
    Nov 15 '18 at 23:42







  • 1





    You can do it either server side or client side. You can just take the first item in your results in javascript and display it at the top of the page since you know it will be the most liked and most recent comment, or you can sort it on the server and send the first row by itself to the client. I'll edit my answer with an example.

    – Diddle Dot
    Nov 15 '18 at 23:50












  • Can you help me with it? im never worked with like and unlike and its pushing my nerves, do you need some code?

    – First Name
    Nov 16 '18 at 0:01











  • It will be similar to how you're listing out the comments in JavaScript already.

    – Diddle Dot
    Nov 16 '18 at 0:24
















Its working! How can I now show most liked post in a one section of website(just one top liked post)? I want most liked post to be displayed in a most liked section I already created, is something like that possible? btw thanks a lot, will flag as asnwer

– First Name
Nov 15 '18 at 23:42






Its working! How can I now show most liked post in a one section of website(just one top liked post)? I want most liked post to be displayed in a most liked section I already created, is something like that possible? btw thanks a lot, will flag as asnwer

– First Name
Nov 15 '18 at 23:42





1




1





You can do it either server side or client side. You can just take the first item in your results in javascript and display it at the top of the page since you know it will be the most liked and most recent comment, or you can sort it on the server and send the first row by itself to the client. I'll edit my answer with an example.

– Diddle Dot
Nov 15 '18 at 23:50






You can do it either server side or client side. You can just take the first item in your results in javascript and display it at the top of the page since you know it will be the most liked and most recent comment, or you can sort it on the server and send the first row by itself to the client. I'll edit my answer with an example.

– Diddle Dot
Nov 15 '18 at 23:50














Can you help me with it? im never worked with like and unlike and its pushing my nerves, do you need some code?

– First Name
Nov 16 '18 at 0:01





Can you help me with it? im never worked with like and unlike and its pushing my nerves, do you need some code?

– First Name
Nov 16 '18 at 0:01













It will be similar to how you're listing out the comments in JavaScript already.

– Diddle Dot
Nov 16 '18 at 0:24





It will be similar to how you're listing out the comments in JavaScript already.

– Diddle Dot
Nov 16 '18 at 0:24



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329035%2fcount-like-and-unlike-php%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党