views:

30

answers:

3

Hi all, I'm very new to web programming, but I have some data that I would really like to present in a constantly updated time series graph on my website. So I have been trying to prototype some code that will display the latest integer value that has been added to a database. The database table I want to reference is being constantly inserted with integer values. My question is this; I'm trying to use a combination of JavaScript and php to display the latest entry into the database when a button on the website is pressed. However the only integer I can get back is the last integer that was present in the database when the page originally loaded. It appears the php function I am using to grab data from the database is run as soon as the page is loaded and does not update after each button press. Is this a limitation of php (or my knowledge of php). Any help would be much appreciated.

(the php function)

function getData()

{ include "config.php";

  $db = mysql_connect($dbhost,$uname,$pass);
  mysql_select_db ($dbname) or die ("Cannot connect");

  $query = "SELECT numCalls FROM $tname ORDER by claatime DESC LIMIT 1";
  $result = mysql_query($query);

  while($r=mysql_fetch_array($result))
  {
     $numCalls = $r["numCalls"];
  }
  return $numCalls;

}

(javascript function)

  function getNum()
  {
     var x = <?php echo getData()?>;
     alert('the latest number is ' +x);
  }
A: 

This is because your page is processed once by PHP, and that's the first time it's requested.

To solve it, put the body of the getData() function in a separate file from the page (we'll call it getData.php). Then make an Ajax request (use a JavaScript library like jQuery to make it easy).

So your <script> section should look like this (assuming you loaded jQuery, see below):

function getNum() {
    $.ajax({
        url: "getData.php",
        complete: function(response) {
            alert("The latest number is " + response.responseText);
        }
    });
}

Your getData.php should look like this:

<?php
include "/home/store/config3.php";

$db = mysql_connect($dbhost,$uname,$pass);
mysql_select_db ($dbname) or die ("Cannot connect to database");


$query = "SELECT numCalls FROM $tname ORDER BY calltime DESC LIMIT 1";
$result = mysql_query($query);

while($r=mysql_fetch_array($result))
{                                                                                                                                                         
    $numCalls = $r["numCalls"];
}

header("Content-Type: text/plain");
print $numCalls;
?>

And to load jQuery simply add

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;

to the top of your page (in the <head> element).

alpha123
I followed what you wrote and everything now works as it should, Obviously I need to fill in a few knowledge gaps with regards to web programming, But this has helped me understand a bit more, thank you very much :)
Steve
No problem. PHP and jQuery are both very easy to use, so it shouldn't take you too long to get up to speed. I recommend you read the W3C tutorials (http://www.w3schools.com/default.asp), they have some easy to follow ones about PHP, SQL, Ajax, jQuery, etc.
alpha123
Right I have been reading a little bit about jQuery and the $.ajax() call is a low level method which as one of its benefits "prevents the browser from caching responses from the server. This can be useful if the server produces its data dynamically". Which is exactly what is needed to provide live data from a database for a real time graph on a website. However I'm having trouble getting the jQuery function to return an integer value instead of the alert box. I would like the integer taken from the database to be returned to the calling function by the jQuery function.
Steve
I have tried adding in a return statement, but it seems to return some kind of object request, and not an integer as I was expecting.
Steve
Well you have 2 options:
alpha123
(sorry hit the enter button by mistake :-P) 1) Do whatever you want to in the complete: function(response).... section and 2) have a variable inside the calling function and assign response.responseText to that variable.
alpha123
A: 

I don't see anything which re-initiates a connection.

Though it gets less known by the second, browsers and websites use a single client-server model; the client receives a reply and everything stops there....unless it re-initiates a connection.

Christian Sciberras
A: 

You don't seem to know the basics of how web technologies work.

Just a very short "crash course":

  1. Browser sends request to a webserver
  2. The webserver runs the PHP script
  3. Whatever the output of your script is gets send back to the browser (this includes your javascript code)
  4. Now the browser shows the page and executes the javascript

As you see, for the integer to be updated you need to make a new request to webserver. You might want to look into Ajax and XMLHttpRequest to do that. There are enough scripts and documentation available that show you how this is done. I don't think you should include a framework merely for that purpose.

slosd