1

hi guys i am trying to create a like system where when users like a post it gets saved in the database that this user has liked this post now i need to count the no of times 'like' has been done.now i used a column type to save the like whenever someone click but when i am retrieving how many likes are on a post how can i count how many likes has been done on this post.here is my code

script

function select_likes(post_id)
{
  var Post_id=post_id;
  var User_id = $('.id_data').attr('value');
  jQuery.ajax({
                  type:'POST',
                  url:'<?php echo base_url("user/select_likes"); ?>',
                  data: {Post_id:Post_id,User_id:User_id},
                  dataType: 'json', 
                   success:function(data)
                  {
                    var ParsedObject = JSON.stringify(data);            
                         var json = $.parseJSON(ParsedObject);

                         $.each(json, function (key, data) {
                          var likes=data.type;
                     // alert(likes);
                          var count=likes.length;
                          alert(count);
                //           var post_id=data.post_id;
                //           $post_id=post_id;
                //           // alert(comment);
                //            // // alert(post_id);
                //            // var      div_list=document.getElementById('#comment_div').innerHTML='post_id;
                //           // $("#comment_post_id").attr('value',$post_id);
                           var mediaID ='.likes'+post_id;
                             $(mediaID).append(likes); 
                  });
                }
          });
}

controller

public function select_likes()
{
  $Post_id=$this->input->post('Post_id');
  $User_id=$this->input->post('User_id');

  $this->load->model('Pmodel');
  $select_likes=$this->Pmodel->select_likes_post_id($Post_id,$User_id);
  // print_r($select_likes);
  echo json_encode($select_likes);
}

model

public function select_likes_post_id($Post_id,$User_id)
{
    $this->db->select('*');
    $this->db->from('userpost_likes_share');
    $this->db->where('post_id',$Post_id);    
    $this->db->where('type','like');
    $query=$this->db->get();
    $likes=$query->result_array();

    return $likes;
}

but how can i count it ? is it possible to count no of times a variable calls in javscript

1 Answer 1

2

simply use count() function on $select_likes in controller count($select_likes) ... it will be the number of like on the post and pass it as json as json_encode(array('like_count'=>$like_count))

So in controller code can be like:

public function select_likes()
{
 $Post_id=$this->input->post('Post_id');
  $User_id=$this->input->post('User_id');

  $this->load->model('Pmodel');
  $select_likes=$this->Pmodel->select_likes_post_id($Post_id,$User_id);
  $like_count = count($select_likes);
  echo json_encode(array('like_count'=>$like_count));
}

Use in js script you can use the data.like_count as total number of like on the post.

If you only need like count from table then i would suggest only select one column from your 'userpost_likes_share' table.. you can select only id(primary key) of the table for make the code work faster.

Let me know if you think i get wrong and answer in wrong direction...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.