I have made a leaderboard in a web app using datas fetched from mysql database in php.The webpage has other functionalities as well with other sql queries.The reference for leaderboard was
what is the best practice leaderboard with php, mysql, memcached? ,
the question that worries me is there a better approach to consider for making leaderboard apps ?
Meanwhile i came across Firebase and was able to run code in Fiddle which is given below :
var LEADERBOARD_SIZE = 6;
var scoreListRef = new Firebase('https://i62qrqslypp.firebaseio-demo.com//scoreList');
var htmlForPath = {};
function handleScoreAdded(scoreSnapshot, prevScoreName) {
var newScoreRow = $("<tr/>");
newScoreRow.append($("<td/>").append($("<em/>").text(scoreSnapshot.val().name)));
newScoreRow.append($("<td/>").text(scoreSnapshot.val().score));
htmlForPath[scoreSnapshot.key()] = newScoreRow;
// Insert the new score in the appropriate place in the table.
if (prevScoreName === null) {
$("#leaderboardTable").append(newScoreRow);
}
else {
var lowerScoreRow = htmlForPath[prevScoreName];
lowerScoreRow.before(newScoreRow);
}
}
function handleScoreRemoved(scoreSnapshot) {
var removedScoreRow = htmlForPath[scoreSnapshot.key()];
removedScoreRow.remove();
delete htmlForPath[scoreSnapshot.key()];
}
var scoreListView = scoreListRef.limitToLast(LEADERBOARD_SIZE);
scoreListView.on('child_added', function (newScoreSnapshot, prevScoreName) {
handleScoreAdded(newScoreSnapshot, prevScoreName);
});
scoreListView.on('child_removed', function (oldScoreSnapshot) {
handleScoreRemoved(oldScoreSnapshot);
});
var changedCallback = function (scoreSnapshot, prevScoreName) {
handleScoreRemoved(scoreSnapshot);
handleScoreAdded(scoreSnapshot, prevScoreName);
};
scoreListView.on('child_moved', changedCallback);
scoreListView.on('child_changed', changedCallback);
$("#scoreInput").keypress(function (e) {
if (e.keyCode == 13) {
var newScore = Number($("#scoreInput").val());
var name = $("#nameInput").val();
$("#scoreInput").val("");
if (name.length === 0)
return;
var userScoreRef = scoreListRef.child(name);
userScoreRef.setWithPriority({ name:name, score:newScore }, newScore);
}
});
Output Leaderboard:
Im new to Firebase as well,please guide me on the issue