Page 1 of 1
Getting data in real time
Posted: Mon Dec 03, 2012 2:41 pm
by overras
Hello Indie Resource! Well, soon I will finish the cPanel of my platform where the games will run and I don't want to refresh the page to update a value . Good, what I need from you guys is the most simple method to get data in real time, update in real time etc.
Example: Halls have 500 golds, I, from cpanel I give him 100 golds so halls now have 600 golds in database but 500 on game screen. Well, what can I do for halls to see the true value of his gold ?
Code: Select all
<html>
...
<div id="gold"><?php $this->gold ?></div>
</html>
I hope you undestand what I wanted to say, thank you very much, remember : THE MOST SIMPLE method, and an example if you can write please, thanks!
Re: Getting data in real time
Posted: Mon Dec 03, 2012 6:38 pm
by hallsofvallhalla
Use ajax. It basically is a Javascript function that queries a PHP page on the server in the background then returns the data without a refresh. I am pretty sure I have a video on it.
Re: Getting data in real time
Posted: Mon Dec 03, 2012 6:43 pm
by overras
Hmm, ok I will search that video on your youtube channel , if I don't found , I will make a post here.
Re: Getting data in real time
Posted: Mon Dec 03, 2012 6:54 pm
by overras
Well, how I expected, I didn't found the video, that video is one of "how to make a rpg/rts with javascript" ? If yes, damn I don't have time to search in every video that function. Another suggestions , opinions ?
Re: Getting data in real time
Posted: Mon Dec 03, 2012 10:53 pm
by Callan S.
I'd like just a simple tutorial (even just a text one) describing how to do overras's gold example. Generally once I know how to manipulate one number I can extrapolate the information to larger projects. So I'll add another vote next to overras's for it!
Re: Getting data in real time
Posted: Tue Dec 04, 2012 7:55 am
by Elvar
It's really not that difficult as it sounds, and it's even easier with jQuery.
What you need is a PHP script which takes care of fetching the data.
server.php (Dummy code, and you will prolly be using PDO etc.)
Code: Select all
$query = mysql_query(fetch some data, where uid == $_POST['uid']);
$row = mysql_fetch_row($query);
print $row['gold'];
So running this script, giving the post query string is set, will print out gold.
Next we need a Javascript that polls this page.
data_fetcher.js
Code: Select all
// We need to keep polling, as we do not know if there is any data to fetch.
setTimeout(function() {
// Let's use jQuery's Ajax API, which makes it a walk in the park.
$.ajax({
type: "POST",
url: "server.php", // Correct the path.
data: { uid: 3 } // Set the user id here.
}).done(function( data ) {
// In the data variable, we get whatever, was printet in server.php
$('.gold').html(data); // Update the gold, in markup.
});
// Poll, every 5000 miliseconds (5 seconds).
}, 5000);
This code is not tested at all, but should give you a good ide how you can handle it.

Re: Getting data in real time
Posted: Tue Dec 04, 2012 9:46 pm
by overras
Re: Getting data in real time
Posted: Tue Dec 04, 2012 11:47 pm
by Callan S.
I will try that out soon, Elvar! Thanks!
Edit: Not sure how to print the value out? I'm not really familiar with javascript?
Re: Getting data in real time
Posted: Wed Dec 05, 2012 3:36 pm
by Jackolantern
That one works, too, but is quite different from the code Elvar posted. That tutorial is using the load() function of AJAX, which loads in the entire contents of the output from the PHP script. The way Elvar is doing it is the more popular way since it is much more flexible. You can do whatever processing you need on the returned data on the client-side in JS, and then put the results where ever you want. However, load() is about as easy as an AJAX call can get, since it is just loading the results of the PHP script directly into the HTML element with the ID of "shoutbox". However, that may work for this usage, since you could just load the amount of gold the player has directly into the HTML element that displays the gold amount.
Re: Getting data in real time
Posted: Wed Dec 05, 2012 6:48 pm
by overras
Yes but, my project is something like this:
Code: Select all
<html>
<!-- Template etc -->
<?php
$lib->load_module();
?>
<!-- End template -->
</html>
And, in the browser bar is a link like this:
http://localhost/pag=module&sub=
page
$_GET['pag'] -
folder with php files
$_GET['sub'] -
php file from "pag" module (folder)
I don't use separated pages , example: <?php include 'right-content.php'; ?>. I use only one connection to database for every file. So , I am supposed to load the entire page right ?
So, what method you recommand ?