views:

123

answers:

3

Hi friends,

I'm not very skilled in web developing by the moment but i've managed to create a web application with PHP that queries a MYSQL Database and shows data in HTML.

I would like to check if the table has changed using a timestamp field that I already created and that updates on every insertion. Comparing two timestamps (the previous and the current) is the key,

but I don't handle javascript that much.

How can I pass a parameter to my hastablechanged.php file (the previous timestamp) and simply return a flag (1: changed, 0: not changed) to javascript? and... how would be a basic javascript function to run a timer and call hastablechanged.php posting old_timestamp and receiving the flag and then update the div?

I've been googling but the solutions I have found are very complex and I know i just need a javascript function that I don't find out how to write.

This is hastablechanged.php:

<?php 
include 'config.php';
include 'opendb.php';

$table = "data_table";
$query_timestamp = "select timestamp from {$table} where id = 0;";
$timestamp = mysql_query("{$query_timestamp}");
if (!$timestamp) {
die("query failed");
}

$row_timestamp = mysql_fetch_array($timestamp);
echo $row_timestamp['timestamp'];
?>

And this is my index.php

<!DOCTYPE HTML PUBLIC>


<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <title>Data values</title>
</head>

<body>

<div id=data>
    <? include("table.php");?>
</div>
</body>

</html>

where "table.php" is the php code that selects the data and displays it drawing a html table.

I would thank a lot any help, as I'm stuck in this issue for finishing my degree project

+1  A: 

You need Ajax and setInterval.

Here is how SetInterval works http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/

SetInterval basically just tells the browser to call a javascript function every x milliseconds.

Here is a few Ajax Examples http://www.w3schools.com/Ajax/ajax%5Fexamples.asp

Ajax is basically a way for javascript to request a page and get its contents without reloading the page. In the example below it tries to create an xmlhttprequest for all the browsers (sadly it's done like this) and then sends in the request. We define state_change as the function to be called when we get a reply back. In this example it just takes the response and displays it, but you can do whatever you want with that.

Below is a modified example. It should work.

<html><head>
<script type="text/javascript">
var xmlhttp;
function loadPage()
{
xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for IE7, Firefox, Opera, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
  xmlhttp.onreadystatechange=state_Change;
  xmlhttp.open("GET","hastablechanged.php?tablename=awesometable",true);
  xmlhttp.send(null);
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }
}

function state_Change()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"
    document.getElementById('A1').innerHTML=xmlhttp.responseText;
    }
  else
    {
    alert("Problem retrieving ata:" + xmlhttp.statusText);
    }
  }
}
</script>
</head>

<body onLoad="setInterval('loadPage()', 10000)">

<p><b>Status:</b>
<span id="A1"></span>
</p>

<button onclick="loadPage()">Check Updates</button>

</body>
</html>
Gus
Thanks a lot :) I'll use this code to build the solution to the table changing contents. Hope it works. Thanks again! :)
echedey lorenzo
A: 

Just to be clear: you don't have to use ajax. Just reloading the page on regular interval will give you what you want. BUT if you want the application to be smooth - then you'll have to dig into AJAX as outlined in the other answers.

-CF

ChronoFish
If he reloads the page on a regular interval then it doesn't need to check if it's been modified and doing automatic page refreshes is a horrible practice that died in the 90s.
Gus
Yeah that is, flickering is not good at all. I started my web application with a meta refresh, but realized that it's a ugly solution for showing database data.
echedey lorenzo
A: 

Hi friends,

I finally figured out how to do it. Posting it so it can be useful for anybody.

It's a combination of php and javacript where:

  • php file return_timestamp.php simply returns the timestamp of a row with ID=0 (no real ID, it simply updates its timestamp in each insertion with a trigger)

  • javascript compares the received timestamp with the one previously received (the very first one is set to 0 so it updates the div in the beginning)

so:

javascript code which is the important thing (i assume you guys can code php retrieving data from mysql):

<script type="text/javascript">
$(document).ready(function() {

    $timestamp1='0';
    var refreshId = setInterval(function() {
    $.get(('return_timestamp.php', function(data) {
    $timestamp2 = data; } )

    if ( $timestamp2 != $timestamp1 ) { 

        $('#data').load('data.php');

        $timestamp1 = $timestamp2; }

}, 1000);
});
</script>

Hope it helps, and thanks as well for YOUR help!

echedey lorenzo