I try to create a personal message system with a 'New-Message'-Counter... For the Counter I have made a MySQL-Function:
function check_unread_messages($chat_id, $from_id) {
return (mysql_result(mysql_query("SELECT COUNT(*) FROM `messages` WHERE `group_id` = $chat_id AND `from_id` = $from_id AND `read_on` is NULL"), 0));
}
And this is, where the function gets called:
jQuery(document).ready(function() {
jQuery(setInterval(function() {
friends_count = document.getElementsByClassName("user").length;
for(var i=1; i < friends_count+1; i++) {
el = document.getElementById(i);
conv_id = el.getAttribute("data-user-id");
user_id = el.getAttribute("data-conv-id");
my_user_id = "<?php echo $session_user_id; ?>";
cache = "check_unread_messages("+conv_id+", "+user_id+")";
countMessages = "<?php "+cache+" ?>";
if(countMessages > 99) {
countMessages = "99+";
}
if(countMessages > 0 || countMessages == "99+") {
document.getElementById("badge-"+i).innerHTML = countMessages;
}
}
}, 500));
});
And this is the Container for the Count:
<div class="user" id='<?php echo $i ?>' data-user-id='<?php echo $user_id?>' data-conv-id='<?php echo $conversation['id'] ?>'>
// something, that doesn't matter
<?php // normaly php gets opend in "something, that doesn't matter"
echo "<div class='rightCountUnreadMessages'>";
echo '<div class="badge" id="badge-'.$i.'"></div>';
echo "</div>";
?>
</div>
Now there comes my Problem... If I try to debug the site, output as much information as I can, and so on... I don't get, what I'm doing wrong...
In the Javascript Function my user id user_id and conv_id is right but I guess, that there is something wrong with the 2 lines after that ('cache = ""...')
Can you pls help me to fix this or give me a better solution? :s (Sorry for my bad english, I'm not a native speaker...)