Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Build Gamified Websites with PHP and jQuery

You're reading from   Build Gamified Websites with PHP and jQuery Using gaming principles to make learning more engaging, motivating, and interactive is a growing trend in web development. It's called gamification, and this book is the complete introduction to its theory and practice.

Arrow left icon
Product type Paperback
Published in Sep 2013
Publisher Packt
ISBN-13 9781782168119
Length 124 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Detrick DeBurr Detrick DeBurr
Author Profile Icon Detrick DeBurr
Detrick DeBurr
Arrow right icon
View More author details
Toc

Players currently online


Let's build out the code to get and show a list of players that are currently online. Here we write a JavaScript function that calls our PHP module to return a list of players that are logged in.

The JavaScript function is as follows:

function GetPlayersOnlineNow(){
var action='GetPlayersOnlineNow',Username='',  html="<table>";
$.ajax({
dataType: "json",
data: {action:action},
url: $AJAXHANDLER,
        success: function (data){
    for (var i = 0; i < data.length; i++) { 
    if(data[i].UserName==''){Username='Anonymous'}else{Username=data[i].UserName};
    html+='<tr><td><a href="#">'+Username +'</a></td></tr>';  
    }
    html+="</table>";
    $("#playersonlinenowdiv").append(html);
  },
        error: function (xhr, status, error) {
            alert(status + error + xhr.responseText);
        }
    });
  
}

The PHP code is as follows:

function GetPlayersOnlineNow(){
$storedProcedure = "Select  UserName from Player...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image