[英]how to count no of times a variable is in a array in javascript in codeigniter?
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
是否有可能不计算变量在javscript中调用的次数
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))
只需在控制器
count($select_likes)
$select_likes
上使用count()
函数...它将是帖子上的like数量,并将其作为json
传递为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. 在js脚本中使用,您可以将
data.like_count
用作帖子上的like总数。
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. 如果您只需要从表中计数,那么我建议您仅从“ userpost_likes_share”表中选择一列。.您可以仅选择表的id(主键)以使代码工作更快。
Let me know if you think i get wrong and answer in wrong direction... 让我知道您是否认为我做错了并且以错误的方向回答...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.