Getting data in real time

For discussions about game development that does not fit in any of the other topics.
Post Reply
overras
Posts: 91
Joined: Sun Oct 03, 2010 7:25 am

Getting data in real time

Post 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!
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Getting data in real time

Post 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.
overras
Posts: 91
Joined: Sun Oct 03, 2010 7:25 am

Re: Getting data in real time

Post by overras »

Hmm, ok I will search that video on your youtube channel , if I don't found , I will make a post here.
overras
Posts: 91
Joined: Sun Oct 03, 2010 7:25 am

Re: Getting data in real time

Post 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 ?
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Getting data in real time

Post 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!
User avatar
Elvar
Posts: 86
Joined: Sun Oct 07, 2012 7:04 pm

Re: Getting data in real time

Post 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. :-)
overras
Posts: 91
Joined: Sun Oct 03, 2010 7:25 am

Re: Getting data in real time

Post by overras »

Sorry for late guys. I found this: http://jquery-howto.blogspot.ro/2009/04 ... conds.html

What you think?
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Getting data in real time

Post 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?
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Getting data in real time

Post by Jackolantern »

overras wrote:Sorry for late guys. I found this: http://jquery-howto.blogspot.ro/2009/04 ... conds.html

What you think?
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.
The indelible lord of tl;dr
overras
Posts: 91
Joined: Sun Oct 03, 2010 7:25 am

Re: Getting data in real time

Post 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 ?
Post Reply

Return to “General Development”