In this code, after clicking the like button, the data is added in the database already. What I wanted to do now is I would like after adding the data, I will query the tota
Try this.
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']);
$no_likes = $this->photoCheese_model->get_like_total($data,$upload);
$result['no_likes'] = $no_likes;
$this->output->set_content_type('application/json')->set_output(json_encode($result));
}
public function get_like_total($data,$uplaod){
$success = $this->db->insert('tbl_like',$data);
if($success){
$this->db->select('SUM(`like`) as no_likes');
$this->db->where('uploadID',$upload);
$query = $this->db->get('tbl_like');
$res = $query->row_array();
return $res['no_likes'];
}
return 0;
}
After all the helps and research. This is the running code of this problem.
In the View:
<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>
Javascript:
<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>
Controller:
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;
}
Model:
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;
}
This code runs perfectly now. Thanks for the help anyway.
public function article_search($search){
$word = explode(' ',$search);
$query = "select c.*,d.name,d.image_link from tbl_product as d inner join tbl_order as c on d.id=c.product_id
where 1 and ";
$i=1;
foreach($word as $wd){
if($i==1){
$query.= "( c.customer like '%".$wd."%'";
}else{
$query.= " or c.customer like '%".$wd."%'";
}
$i++;
}
foreach($word as $wd){
if($i==1){
$query.= "( c.order_no like '%".$wd."%'";
}else{
$query.= " or c.order_no like '%".$wd."%'";
}
$i++;
}
foreach($word as $wd){
if($i==1){
$query.= "( d.name like '%".$wd."%'";
}else{
$query.= " or d.name like '%".$wd."%'";
}
$i++;
}
foreach($word as $wd){
if($i==1){
$query.= "( d.commission like '%".$wd."%'";
}else{
$query.= " or d.commission like '%".$wd."%'";
}
$i++;
}
foreach($word as $wd){
if($i==1){
$query.= "( d.base_price like '%".$wd."%'";
}else{
$query.= " or d.base_price like '%".$wd."%'";
}
$i++;
}
$query.= " or c.customer like '%".$search."%'";
$query.= " or c.order_no like '%".$search."%' ";
$query.= " or d.name like '%".$search."%' ";
$query.= " or d.commission like '%".$search."%' ";
$query.= " or d.base_price like '%".$search."%' )";
$query = $this->db->query($query);
//echo $this->db->last_query();die;
$record = $query->result_array();
foreach($record as $key=>$value){
$cat = $value['product_id'];
$query_tag = $this->db->query("select * from tbl_product where id in(".$cat.")");
$record[$key]['all_pro'] = $query_tag->result_array();
}
return $record;
}
public function like_total(){
$data = array('like' => 1,
'userID'=>$this->session->userdata('userID'),
'uploadID'=>$_GET['like_id']);
$no_likes = $this->photoCheese_model->get_like_total($data,$this->input->get('like_id'));
$result['no_likes'] = $no_likes;
$this->output->set_content_type('application/json')->set_output(json_encode($result));
}
public function get_like_total($data,$this->input->get('like_id')){
$success = $this->db->insert('tbl_like',$data);
if($success){
$this->db->select('SUM(`like`) as no_likes');
$this->db->where('uploadID',$this->input->get('like_id'));
$query = $this->db->get('tbl_like');
$res = $query->row_array();
return $res['no_likes'];
}
return 0;
}
This looks like a decent code to me
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);
}
Just one try.. change your
return json_encode($result)
to
echo json_encode($result)
This example may help you in the future jquery ajax php example