我正在尝试获取不同功能的计数值,然后显示值。我的最终目标是获取通知计数当用户喜欢某人发布时有人接受您的请求以及有人评论了帖子我需要在相同的通知徽章中获取所有三个通知,所以需要添加所有功能的计数并将最后的结果提供给badge.let我告诉你我想要做什么 第一个功能
function recieve_accept_notification()
{
var id = $('.id_data').attr('value');
// var Recievers_id = $('.id_data').attr('value');
// var senders_id = $('.senders_div').attr('senders_id');
jQuery.ajax({
type: 'POST',
url: '<?php echo base_url('user/user_recieve_return_requests'); ?>',
data: {
id: id,
},
dataType:'json',
success: function(data) {
// alert(data);
if(data=="")
{}
else{
var ParsedObject = JSON.stringify(data);
var count='0';
var recievers_values = $.parseJSON(ParsedObject);
$.each(recievers_values, function(key, data) {
count++;
var uname = data.uname;
var senders_id = data.friend_id;
var Recievers_id = data.user_id;
var friends_request_image=data.image;
{
$('.recieve_accept_notification').append('<a href="<?php echo base_url('user/profile_friend'); ?>/'+Recievers_id+'" id=rec' + Recievers_id + ' ><img class="img-circle" height="25px" width="25px" style="margin-top: 8px;" src= "<?php echo base_url('uploads'); ?>/'+friends_request_image+'"> <strong>' + uname + ' ' + '</strong>accepted your friend Request <div style="padding-left: 11em;"></center></div></a>');
}
});
$('#notification_count').val(count); //this sends the no of counted values to the html input
}
} });
}
function when_post_like_notification()
{
var id = $('.id_data').attr('value');
jQuery.ajax({
type: 'POST',
url: '<?php echo base_url('user/user_get_posts_id'); ?>',
data: {
id: id,
},
dataType:'json',
success: function(data) {
var ParsedObject = JSON.stringify(data);
var recievers_values = $.parseJSON(ParsedObject);
$.each(recievers_values, function(key, data) {
// alert(recievers_values);
var Recievers_id = data.id;
// alert(Recievers_id);
jQuery.ajax({
type: 'POST',
url: '<?php echo base_url('user/user_liked_post_notification'); ?>',
data: {
id: Recievers_id,
},
dataType:'json',
success: function(data) {
// alert(data);
if(data=="")
{}
else{
var ParsedObject = JSON.stringify(data);
var recievers_values = $.parseJSON(ParsedObject);
var count='0';
$.each(recievers_values, function(key, data) {
count++;
var uname = data.uname;
var Recievers_id = data.user_id;
var friends_request_image=data.image;
$('.recieve_accept_notification').append('<a href="<?php echo base_url('user/profile_friend/');?>/'+Recievers_id+'" id=rec' + Recievers_id + ' ><img class="img-circle" height="25px" width="25px" style="margin-top: 8px;" src= "<?php echo base_url('uploads'); ?>/'+friends_request_image+'"> <strong>' + uname + ' ' + '</strong>liked your post.<div style="padding-left: 11em;"></center></div></a>');
});
var notification_count= $('#notification_count').val();
var new_count= parseInt(notification_count)+ parseInt(count) ;
this adds the previous value from the notification and shows the result in for next .
$('#notification_count').val(new_count);
}
}
});
});
}
});
}
function when_someone_commented_onpost()
{
// alert();
var id = $('.id_data').attr('value');
jQuery.ajax({
type: 'POST',
url: '<?php echo base_url('user/user_get_posts_id'); ?>',
data: {
id: id,
},
dataType:'json',
success: function(data) {
console.log(data);
if(data="")
{}
else{
var ParsedObject = JSON.stringify(data);
var recievers_values = $.parseJSON(ParsedObject);
$.each(recievers_values, function(key, data) {
var Recievers_id = data.id;
jQuery.ajax({
type: 'POST',
url: '<?php echo base_url('user/user_get_all_comments_by_someone'); ?>',
data: {
id: Recievers_id,
},
dataType:'json',
success: function(data) {
var ParsedObject = JSON.stringify(data);
var recievers_values = $.parseJSON(ParsedObject);
var count= '0';
$.each(recievers_values, function(key, data) {
count++;
var uname = data.uname;
var Recievers_id = data.user_id;
var friends_request_image=data.image;
$('.recieve_accept_notification').append('<a href="<?php echo base_url('user/profile_friend/');?>/'+Recievers_id+'" id=rec' + Recievers_id + ' ><img class="img-circle" height="25px" width="25px" style="margin-top: 8px;" src= "<?php echo base_url('uploads'); ?>/'+friends_request_image+'"> <strong>' + uname + ' ' + '</strong>commented on your post.<div style="padding-left: 11em;"></center></div></a>');
});
var notification_count= $('#notification_count').val();
var new_count=parseInt(notification_count)+ parseInt(count) ;
$('#notification_count').val(new_count);
}
});
});}
}
});
}
function add_all_counts()
{
value=$('#notification_count').val();
// alert(value);
this is the final function that shows the value to the span and give us the notification
$('#notification_count').val(value);
$('#accept_request_count').html('<span class="badge" >'+value+'</span>');
}
这是显示结果的html区域
<span style="font-size: 1.3em; z-index: 9999999;" class="fa fa-globe" id="accept_request_count" ></span> <input type="hidden" name="notification_count" id="notification_count" value="0">
让我向您展示一些可以更好地解释的图片