简体   繁体   English

如何在Codeigniter中的javascript数组中不计算变量的次数?

[英]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.

相关问题 计算字符在字符串中出现的次数,并将其存储在数组JavaScript中 - Count how many times a character happens in a string and store it in an array JavaScript 如何计算变量在javascript数组中重复的次数? - How to count the number of times variables repeats in a javascript array? 计算值在数组中出现的次数-Javascript-angular-lodash - Count how many times a value occurs in an array - javascript - angular - lodash 使用javascript / jquery从现有数组创建一个新数组,该数组保存一个项目存在的次数 - Create a new array from an existing array that holds the count for how many times an item exist using javascript/jquery 如何计算项目在数组中的次数 - How to count number of times an item is in an array 计算 javascript 数组中相同值出现的次数 - Count the number of times a same value appears in a javascript array Javascript数组计数并将结果添加到变量 - Javascript Array count and add result to variable 如何通过jQuery $ .window url将javascript数组变量传递给codeigniter控制器? - How to pass javascript array variable through jquery $.window url to codeigniter controller? 如何计算数组中的项目 - javascript - how to count items in array - javascript 如何计算 JavaScript 数组对象? - How to count JavaScript array objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM