在此代码中,单击“赞”按钮后,数据已添加到数据库中。我现在想做的是添加数据后,我将查询所选项目的总数,并在不加载页面的情况下显示它。
这是我现在的代码:
我的看法:
<p id='state'><i class='fa fa-thumbs-up'></i><span id="likeThis"><?php echo $countLike;?></span> likes • <i class='fa fa-thumbs-down'></i><?php echo $countDisLike;?> dislikes •<i class='fa fa-thumbs-down'></i><a href='<?php echo base_url();?>index.php/photoCheese/deleteUploadPic/<?php echo $row['uploadID'];?>'>Delete Picture</a></p> <input type="button" onclick="getVal(this.value)" class='detailButton1' name='like_name' id='like_id' value='<?php echo $link;?>' title='Like this post'><i class='fa fa-thumbs-up'></i> Like</input>
Javascript:
function getVal(value) { jQuery.ajax({ type:"GET", url: "<?php echo base_url();?>index.php/photoCheese/like_total/", dataType:'json', data: {like_id : value}, success: function(res){ alert(res.no_likes); if(res){ jQuery("#likeThis").html(res.no_likes); } } });
控制器:
public function like_total(){ $id = $this->session->userdata('userID'); $upload = $this->input->get('like_id'); $data = array('like' => 1, 'userID'=>$id, 'uploadID'=>$_GET['like_id']); $result = $this->photoCheese_model->get_like_total($data,$upload); return json_encode($result); }
模型:
public function get_like_total($data,$uplaod){ $success = $this->db->insert('tbl_like',$data); if($success){ $this->db->select('uploadID,SUM(`like`) as no_likes',false); $this->db->where('uploadID',$upload); $this->db->where('like !=',2); $query = $this->db->get(); } return $query->result_array(); }
此代码将不会显示total_likes。这是怎么了
毕竟有帮助和研究。这是此问题的运行代码。
在视图中:
<p id='state'><i class='fa fa-thumbs-up'></i><span class="likeThis"><?php echo $countLike;?></span> likes • <i class='fa fa-thumbs-down'></i><?php echo $countDisLike;?> dislikes •<i class='fa fa-thumbs-down'></i><a href='<?php echo base_url();?>index.php/photoCheese/deleteUploadPic/<?php echo $row['uploadID'];?>'>Delete Picture</a></p> <input type="button" onclick="getVal(this.value)" class='detailButton1' name='like_name' id='like_id' value='<?php echo $link;?>' title='Like this post'><i class='fa fa-thumbs-up'></i> Like</input>
<script type="text/javascript"> function getVal(value) { jQuery.ajax({ type:"GET", url: "<?php echo base_url();?>index.php/photoCheese/like_total/", dataType:'json', data: {like_id : value}, error: function(result){ $('.likeThis').append('<p>goodbye world</p>'); }, success: function(result){ jQuery(".likeThis").html(result); } }); } </script>
public function like_total(){ $id = $this->session->userdata('userID'); $upload = $this->input->get('like_id'); $data = array('like' => 1, 'userID'=>$id, 'uploadID'=>$_GET['like_id']); $result = $this->photoCheese_model->get_like_total($data,$upload); $this->output->set_content_type('application/json'); $this->output->set_output(json_encode($result)); return $result; }
public function get_like_total($data,$upload){ $success = $this->db->insert('tbl_like',$data); //Query the total likes if($success){ $this->db->select()->from('tbl_like'); $this->db->where('uploadID',$upload); $this->db->where('like !=',2); $query = $this->db->get(); return $query->num_rows(); } return 0; }
该代码现在可以完美运行。无论如何,谢谢您的帮助。