Reputation: 31
I am creating twitter clone using MVC architecture. I have written following javascript code to display like and unlike button.
When user clicks like button I am passing tweetId to ajax to update database using actions.php,
$(".likeTweet").click(function() {
var id = $(this).attr("data-tweetlikeId"); /* Id of tweet*/
$.ajax({
type: "POST",
url: "actions.php?action=likeTweet",
data: "tweetlikeId=" + id,
success: function(result){
if (result == "1") {
$("a[data-tweetlikeId='" + id + "']").html("<p>Unlike</p>"); /* Displays Unlike button and I want to display like count here */
} else if (result == "2") {
$("a[data-tweetlikeId='" + id + "']").html("<p>Like</p>"); /* Displays like button and I want to display like count here */
}
}
})
})
Below code is from actions.php where It checks if user($_SESSION['id') has already liked the tweet(tweetlikeId) if yes then it deletes the entry from Database and return value 1.
If user has not liked tweet before then it will add an entry to database and return value 2
if ($_GET['action'] == 'likeTweet') {
$query = "SELECT * FROM tweetLikes WHERE userid = ". mysqli_real_escape_string($link, $_SESSION['id'])." AND tweetid = ". mysqli_real_escape_string($link, $_POST['tweetlikeId'])." LIMIT 1";
$result = mysqli_query($link, $query);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
mysqli_query($link, "DELETE FROM tweetLikes WHERE userid = ". mysqli_real_escape_string($link, $_SESSION['id'])." AND tweetid = ". mysqli_real_escape_string($link, $_POST['tweetlikeId'])." LIMIT 1");
echo "1";
} else {
mysqli_query($link, "INSERT INTO tweetLikes (userid, tweetid) VALUES (". mysqli_real_escape_string($link, $_SESSION['id']).", ". mysqli_real_escape_string($link, $_POST['tweetlikeId']).")");
echo "2";
}
$queryCount = "SELECT COUNT(tweetid) AS likes FROM tweetLikes WHERE tweetid = ". mysqli_real_escape_string($link, $_POST['tweetlikeId'])."";
$queryResult = mysqli_query($link, $queryCount);
$rowCount = mysqli_fetch_assoc($queryResult);
$count = $rowCount['likes']; /* Count of likes for a tweet */
}
In actions.php I am also making count of likes for that tweet. But I want to display count of likes for that particular tweet using Javascript beside like & unlike button.(In $(".likeTweet").click(function())
Can somebody tell me how to fetch like count from actions.php, send it to Ajax and display it in Javascript beside like/unlike button ???
Upvotes: 1
Views: 224
Reputation: 3240
Hi you can do this by passing an array to json encode function. Here is the updated javascript file
$(".likeTweet").click(function() {
var id = $(this).attr("data-tweetlikeId"); /* Id of tweet*/
$.ajax({
type: "POST",
url: "actions.php?action=likeTweet",
data: "tweetlikeId=" + id,
dataType: "json",
success: function(result){
if (result.likeDislike == "1") {
// $("a[data-tweetlikeId='" + id + "']").html("<p>Unlike</p>"); /* Displays Unlike button and I want to display like count here */
$("a[data-tweetlikeId='" + id + "']").html(result.likeCount); /* Displays Unlike button and I want to display like count here */
} else if (result.likeDislike == "2") {
// $("a[data-tweetlikeId='" + id + "']").html("<p>Like</p>"); /* Displays like button and I want to display like count here */
$("a[data-tweetlikeId='" + id + "']").html(result.likeCount); /* Displays like button and I want to display like count here */
}
}
})
})
and here is the updated action.php file.
if ($_GET['action'] == 'likeTweet') {
$query = "SELECT * FROM tweetLikes WHERE userid = ". mysqli_real_escape_string($link, $_SESSION['id'])." AND tweetid = ". mysqli_real_escape_string($link, $_POST['tweetlikeId'])." LIMIT 1";
$result = mysqli_query($link, $query);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
mysqli_query($link, "DELETE FROM tweetLikes WHERE userid = ". mysqli_real_escape_string($link, $_SESSION['id'])." AND tweetid = ". mysqli_real_escape_string($link, $_POST['tweetlikeId'])." LIMIT 1");
$likeDislike = "1";
} else {
mysqli_query($link, "INSERT INTO tweetLikes (userid, tweetid) VALUES (". mysqli_real_escape_string($link, $_SESSION['id']).", ". mysqli_real_escape_string($link, $_POST['tweetlikeId']).")");
$likeDislike = "2";
}
$queryCount = "SELECT COUNT(tweetid) AS likes FROM tweetLikes WHERE tweetid = ". mysqli_real_escape_string($link, $_POST['tweetlikeId'])."";
$queryResult = mysqli_query($link, $queryCount);
$rowCount = mysqli_fetch_assoc($queryResult);
$count = $rowCount['likes']; /* Count of likes for a tweet */
echo json_encode(array("likeDislike" => $likeDislike, "likeCount" => $count));
}
Upvotes: 0