I would like to implement this like and unlike into codeigniter. I can do it in the normal php using the following codes but I just don't know how to implement it in codeigniter. any help would be appritiated. thanks.
posts table
CREATE TABLE `posts` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
`content` text NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
like_unlike table
CREATE TABLE `like_unlike` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`postid` int(11) NOT NULL,
`type` int(2) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
database conn
<?php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "tutorial"; /* Database name */
$con = mysql_connect($host, $user, $password) or die("Unable to connect");
// selecting database
$db = mysql_select_db($dbname, $con) or die("Database not found");
View
<div class="content">
<?php
$userid = 5;
$query = "SELECT * FROM posts";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$postid = $row['id'];
$title = $row['title'];
$content = $row['content'];
$type = -1;
// Checking user status
$status_query = "SELECT count(*) as cntStatus,type FROM like_unlike WHERE userid=".$userid." and postid=".$postid;
$status_result = mysql_query($status_query);
$status_row = mysql_fetch_array($status_result);
$count_status = $status_row['cntStatus'];
if($count_status > 0){
$type = $status_row['type'];
}
// Count post total likes and unlikes
$like_query = "SELECT COUNT(*) AS cntLikes FROM like_unlike WHERE type=1 and postid=".$postid;
$like_result = mysql_query($like_query);
$like_row = mysql_fetch_array($like_result);
$total_likes = $like_row['cntLikes'];
$unlike_query = "SELECT COUNT(*) AS cntUnlikes FROM like_unlike WHERE type=0 and postid=".$postid;
$unlike_result = mysql_query($unlike_query);
$unlike_row = mysql_fetch_array($unlike_result);
$total_unlikes = $unlike_row['cntUnlikes'];
?>
<div class="post">
<h1><?php echo $title; ?></h1>
<div class="post-text">
<?php echo $content; ?>
</div>
<div class="post-action">
<input type="button" value="Like" id="like_<?php echo $postid; ?>" class="like" style="<?php if($type == 1){ echo "color: #ffa449;"; } ?>" /> (<span id="likes_<?php echo $postid; ?>"><?php echo $total_likes; ?></span>)
<input type="button" value="Unlike" id="unlike_<?php echo $postid; ?>" class="unlike" style="<?php if($type == 0){ echo "color: #ffa449;"; } ?>" /> (<span id="unlikes_<?php echo $postid; ?>"><?php echo $total_unlikes; ?></span>)
</div>
</div>
<?php
}
?>
</div>
AJAX.php
<?php
include "config.php";
$userid = 5;
$postid = $_POST['postid'];
$type = $_POST['type'];
// Check entry within table
$query = "SELECT COUNT(*) AS cntpost FROM like_unlike WHERE postid=".$postid." and userid=".$userid;
$result = mysql_query($query);
$fetchdata = mysql_fetch_array($result);
$count = $fetchdata['cntpost'];
if($count == 0){
$insertquery = "INSERT INTO like_unlike(userid,postid,type) values(".$userid.",".$postid.",".$type.")";
mysql_query($insertquery);
}else {
$updatequery = "UPDATE like_unlike SET type=" . $type . " where userid=" . $userid . " and postid=" . $postid;
mysql_query($updatequery);
}
// count numbers of like and unlike in post
$query = "SELECT COUNT(*) AS cntLike FROM like_unlike WHERE type=1 and postid=".$postid;
$result = mysql_query($query);
$fetchlikes = mysql_fetch_array($result);
$totalLikes = $fetchlikes['cntLike'];
$query = "SELECT COUNT(*) AS cntUnlike FROM like_unlike WHERE type=0 and postid=".$postid;
$result = mysql_query($query);
$fetchunlikes = mysql_fetch_array($result);
$totalUnlikes = $fetchunlikes['cntUnlike'];
// initalizing array
$return_arr = array("likes"=>$totalLikes,"unlikes"=>$totalUnlikes);
echo json_encode($return_arr);
Jquery
$(document).ready(function(){
// like and unlike click
$(".like, .unlike").click(function(){
var id = this.id; // Getting Button id
var split_id = id.split("_");
var text = split_id[0];
var postid = split_id[1]; // postid
// Finding click type
var type = 0;
if(text == "like"){
type = 1;
}else{
type = 0;
}
// AJAX Request
$.ajax({
url: 'likeunlike.php',
type: 'post',
data: {postid:postid,type:type},
dataType: 'json',
success: function(data){
var likes = data['likes'];
var unlikes = data['unlikes'];
$("#likes_"+postid).text(likes); // setting likes
$("#unlikes_"+postid).text(unlikes); // setting unlikes
if(type == 1){
$("#like_"+postid).css("color","#ffa449");
$("#unlike_"+postid).css("color","lightseagreen");
}
if(type == 0){
$("#unlike_"+postid).css("color","#ffa449");
$("#like_"+postid).css("color","lightseagreen");
}
}
});
});
});
I would be grateful if someone could show me how to implement this in codeigniter Thanks in advance.