I am trying to get the count values of different function to sum up and then show the values.What my end goal is to get the notification count for when user like someone post when someone accepted your request and when someone commented on the post i need to get the notification for all three in the same notification badge so need to add the count from all the functions and give the last result to the badge.let me show you what i am trying to do 1st function
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>');
}
this is the html area that show the result
<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">
let me show you some images that would explain better