我想实现这个like和不同于codeigniter。我可以在普通的php中使用下面的代码来做到这一点,但我不明白为什么它不能在codeigniter中工作,这是我的数据库表和我的模型视图和控制器。任何帮助都将得到满足。谢谢。喜欢不同于codeigniter中的函数
职位表
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;
样不像表
CREATE TABLE `like-unlike` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`post_id` int(11) NOT NULL,
`purpose` int(2) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
jQuery的文件
function likeItem(post_id)
{
if ($("#likeItem_" + post_id).text() == "Like")
{
$("#likeItem_" + postid).html('Unlike');
var purpose = "Like";
} else
{
$("#likeItem_" + post_id).html('Like');
var purpose = "UnLike";
}
$.ajax({
type: "POST",
url: "<?php echo base_url('posts/likes');?>",
data: "postId=" + postId + "&purpose=" + purpose,
success: function (data)
{
// do seomthing here with data
// console.log(data) --> to see data return or not
}
}
);
这是我的模型 “Post_model.php”
public function itemLike() {
$id = $this->session->userdata('user_id');
$postId = $this->input->post('post_id');
$purpose = $this->input->post('purpose');
if ($purpose == "Like") {
// echo 'test';
// exit();
$data = array(
"user_id" => $id,
"post_id" => $postId,
);
$this->db->insert('like', $data);
} else {
echo 'failed';
}
}
这是我的看法文件
<li><a class="like-unlike" href="#" id="likeItem_{$item['post_id']}" onclick="likeItem({$item['post_id']})">Like</a></li>
这是我的控制器 “post.php中”
public function likeItem()
{
$this->usermodel->itemLike();
}
您是否检查日志? –
什么时候是UnLike? –
你试图在你的数据库被调用时发布到'like -like':'like_unlike' –