【发布时间】:2016-08-21 14:09:25
【问题描述】:
查看
我尝试在这里创建类似按钮。每次我点击喜欢按钮时,它都会增加 1。在这里,我如何使用 foreach() 循环从数据库中输入值并在 jquery 中输入它
<script type="text/javascript">
$(document).ready(function(){
$("#like").click(function() {
var id='1069347886434951';//this is not artist who likes
var creation_id= 1;
$.ajax({
type: "post",
url: "<?php echo base_url(); ?>creations/like_creation",
data:'id='+id + '&creation_id=' +creation_id,//after its split, the split function gives an array
success: function(response){
try{
if(response=='true'){
var newValue = parseInt($("#like").text()) + 1;
$("#"+voteId+'_result').html(newValue);// adds the value to no of like on the client side
}else{
alert('Sorry Unable to update..');
}
}catch(e) {
alert('Exception while request..');
}
},
error: function(){
alert('Error while request..');
}
});
});
});
</script>
控制器 控制器将值插入数据库。如果您使用邮递员输入值,它将使用 json upto ajax 返回 true。我已经在邮递员中尝试过了。
public function like_creation(){
//$artist_id=$this->session->userdata('user_id');
$artist_id=$this->input->post('id');
//bring creation id from the database when fed using foreach loop
$creation_id=$this->input->post('creation_id');
//$up_like1 =0;
$data1=array(
'id'=> $this->input->post('id'),//bring it from artist_infors
'creation_id'=> $this->input->post('creation_id'),
'artist_who_likes'=> $artist_id ,
);
$query=$this->hbmodel->insert_like($data1);
$status= "true";
echo $status;
}
型号
public function no_likes($artist_id, $creation_id)
{
$sql="SELECT count(like_id) as num from likes as l where id='$artist_id' and creation_id= $creation_id";
//artist id has to determine whether it is user himself or the one whom he/she tries to follow
$query=$this->db->query($sql);
return $query->result();
}
MySQL 查询
CREATE TABLE IF NOT EXISTS `likes` (
`like_id` int(11) NOT NULL AUTO_INCREMENT,
`id` varchar(500) COLLATE utf16_bin DEFAULT NULL,//the artist id whose creation is fed
`creation_id` int(11) DEFAULT NULL,
`artist_who_likes` varchar(500) COLLATE utf16_bin DEFAULT NULL,
PRIMARY KEY (`like_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_bin AUTO_INCREMENT=29 ;
【问题讨论】:
-
问题是什么?
-
我无法使用上述代码创建类似按钮。你能找出它有什么问题吗
-
错误信息?控制台消息?还有什么是try catch?没有代码会在您的成功中引发异常。你需要返回计数器,不是真假
-
嗯?我认为成功完成了异常处理..有try and catch。并且没有语法错误。只有代码不工作
-
不需要异常处理。 “代码不起作用”不是很有帮助。查看控制台或 PHP 错误时,一定有一些可见的错误
标签: php jquery mysql ajax codeigniter