Reputation: 397
I developed a chat using firebase.
Not want to count all message that has unread status and display it to my list of users.
Here is my database.
As you can see in the image, I have status:"unread"
Here is what I did in javascript.
<script>
var ref = new Firebase("https://mosbeau-cd349.firebaseio.com/Users/<?php echo $ae; ?>/");
ref.on("value", function(snapshot) {
var names="";
var un=0;
for (x in snapshot.val()) {
var xRef = new Firebase("https://mosbeau-cd349.firebaseio.com/Users/<?php echo $ae; ?>/"+x+"/");
var messagesRef = new Firebase("https://mosbeau-cd349.firebaseio.com/Chats/"+x+" <?php echo $ae; ?>");
xRef.once("value", function(xsnapshot) {
var data = xsnapshot.val();
var name = data["cuname"];
//console.log(name);
messagesRef.on("value", function(ysnapshot) {
for (y in ysnapshot.val()) {
var yRef = new Firebase("https://mosbeau-cd349.firebaseio.com/Chats/"+x+" <?php echo $ae; ?>/"+y+"/");
yRef.once("value", function(zsnapshot) {
var ydata = zsnapshot.val();
var status = ydata["status"];
alert(status);
if(status=="unread"){
un++;
}
});
}
});
names += "<li style='padding:10px;border-bottom:1px solid#f2f2f2;'><a href='adminchat.php?id="+x+"&ae=<?php echo $ae; ?>&aeid=<?php echo $aeid; ?>&custo="+name+"'>"+name+" "+un+"</a></li>";
});
}
document.getElementById('results').innerHTML=names;
});
</script>
I can't get the total of unread message.
Please help me.
Upvotes: 2
Views: 7133
Reputation: 7947
Use a query, a very common thing in Firebase:
...
//console.log(name);
// NEW
messagesRef.orderByChild("status").equalTo("unread").on("value", function(ysnapshot) {
var unread = ysnapshot.numChildren();
});
names += "<li style='padding:10px;border-bottom:1px solid#f2f2f2;'><a href='adminchat.php?id="+x+"&ae=<?php echo $ae; ?>&aeid=<?php echo $aeid; ?>&custo="+name+"'>"+name+" "+un+"</a></li>";
...
"https://mosbeau-cd349.firebaseio.com/Chats/"+x+" <?php echo $ae; ?>/"
resolves to something like: "https://mosbeau-cd349.firebaseio.com/Chats/10736 ONLINESHOP/"
So all you're doing is getting this node, 10736 ONLINESHOP
, and retrieving by its children having a child status
with value unread
.
Upvotes: 2