prosportal Posted April 17, 2012 Share Posted April 17, 2012 I'm using jquery ajax to update the number of people currently logged into the site every 3 seconds using this code. function updateStats(stat) { var stat = 'online'; var url = "online.php"; $.post(url,{stat: stat} , function(data) { $("#online").html(data); }) } setInterval('updateStats("updateStats")', 3000); here's the html <body onLoad="updateStats(stats);"> <div id="online">This is updating with the returned PHP</div> <div id="currency">How do I make this update in the same function?</div> Here is the PHP if($_POST['stat'] == 'online'){ $result = $mysqli->query("SELECT loggedin FROM accounts WHERE loggedin != 0"); echo $result->num_rows; } My question is.. I'd like to have other elements updating every so often as well without refreshing. I tried for about an hour to create an array for the stat variable, but don't know enough jquery or ajax to make it happen. Can anyone lend a hand? This is for a gaming server and I'd like to update the total currency in circulation as well, which would be if($_POST['stat'] == 'money') { $result = $mysqli->query("SELECT sum(money) AS totalCurrency FROM players"); $getData = $result->fetch_assoc(); echo number_format($getData['totalCurrency']); } Quote Link to comment https://forums.phpfreaks.com/topic/261074-phpjqueryajax-array/ Share on other sites More sharing options...
Shadowing Posted April 17, 2012 Share Posted April 17, 2012 do you need to return php information for the other element updates? cause i was going to say you can just use java script with out even using ajax for that. Are you not using jquery atm? i use ajax with jquery. its waaaay easier. I think you need to be using this form for ajax stuff. this is what ive been using, im still a noob though. but ive been doing alot of ajax for the past month now. will try to help ya out. function updateStats() { $.ajax({ url: online.php", type: 'POST', dataType: 'json', data: { stat: $('#stat).val(), // variables you want to pass to your php page }, success: function(response) { // grabbing the key from the php page $('#display_element).html(response.display); } }); } Then on your php you make your array like this if($_POST['stat'] == 'online'){$result = $mysqli->query("SELECT loggedin FROM accounts WHERE loggedin != 0"); ;} echo json_encode(array("display" => $result['loggedin'])); i dont use a framework like you are using so im not sure how to type the key out to grab from your loop see this in the java script code, responce.display is grabing the key "display" from the array on the php page. and then shoving it onto the div that is has the id of display_element $('div#display_element).html(response.display); also for data: im sending your $_POST to your php script so your php script can read the variable. what is post stat anyways? is it a form or just a variable you made global? Quote Link to comment https://forums.phpfreaks.com/topic/261074-phpjqueryajax-array/#findComment-1338046 Share on other sites More sharing options...
prosportal Posted April 17, 2012 Author Share Posted April 17, 2012 Thanks for your input. Perhaps I'm asking the wrong question. How would I make THIS work? <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="../jquery.js"></script> <script type="text/javascript"> function updateStats(stat) { var stat = ["online","money"]; var url = "online.php"; $.each(stat,function(stat){ $.post(url,{stat: stat} , function(data) { $("#" + this).html(data); }) }) } setInterval('updateStats("updateStats")', 2000); </script> <body onLoad="updateStats(stats);"> <div id="online"></div> <div id="money"></div> </body> </html> PHP if($_POST['stats']=='online') { $result= $mysqli->query("SELECT loggedin FROM accounts WHERE loggedin !=0"); echo $result->num_rows; } elseif($_POST['stats'] == 'money') { $result = $mysqli->query("SELECT sum(money) AS totalCurrency FROM players"); $getData = $result->fetch_assoc(); echo number_format($getData['totalCurrency']); } Quote Link to comment https://forums.phpfreaks.com/topic/261074-phpjqueryajax-array/#findComment-1338172 Share on other sites More sharing options...
Andy-H Posted April 17, 2012 Share Posted April 17, 2012 If you've already loaded jquery you mayaswell make use of document ready: get_stats.js function get_stats(type, callback) { $.getJSON('get_stats.php', { stat : type }, function(data) { if ( data.qry.toLowerCase() != 'ok' ) { alert(data.response); return false; } callback.call(this, data.response); return false; }); }; <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="../jquery.js"></script> <script type="text/javascript" src="../get_stats.js"></script> <script type="text/javascript"> $(document).ready(function() { var secs = 3; var stats = [{ type : 'online', selector : '#online' }, { type : 'money', selector : '#money' }]; $.each(stats, function(stat) { setInterval(function() { get_stats(stat.type, function(data) { $(stat.selector).html(data); }); }, secs * 1000); }); }); </script> <body> <div id="online"><> <div id="money"><> </body> </html> PHP (get_stats.php) function respond($response, $qry = 'OK') { return json_encode(array('qry' => $qry, 'response' => $response)); } $mysqli = new mysqli('localhost', 'user', 'pass', 'db'); $stats = isset($_GET['stats']) ? $_GET['stats'] : ''; switch($stats) { case 'online' : $result = $mysqli->query("SELECT logged_in FROM accounts WHERE logged_in != 0"); echo respond($result->num_rows); exit; break; case 'money' : $result = $mysqli->query("SELECT sum(money) AS totalCurrency FROM players"); $row = $mysqli->fetch_field_direct(0); echo respond(number_format($row->totalCurrency)); exit; break; case 'default' : echo respond('Invalid arguments', 'FAIL'); exit; break; } Quote Link to comment https://forums.phpfreaks.com/topic/261074-phpjqueryajax-array/#findComment-1338180 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.