Viewed   75 times

How can I detect the latest updates made to a database and silently refresh a page when a change occurs?

Let's say the database access looks like:

$host = "localhost";
$username = "root";
$password = "root";
$db = mysql_connect($host,$username,$password) or die(mysql_error());
mysql_select_db('ccr') or die(mysql_error());

Any ideas and samples would be appreciated. Thank you.

 Answers

1

This is how I recently implemented a solution using jQuery.

PHP increments a field in the database every time a significant update occurs.

<?php

//  Call this function when data changes
function update_clients()
{
    mysql_query( "UPDATE pageGen SET id = id + 1 LIMIT 1" );
}

//  Call this function to get the ID to pass to JavaScript
function get_update()
{
    $result = mysql_query( "SELECT id FROM pageGen LIMIT 1" );
    $update = mysql_result( $result, 0, 'id' );
    return $update;
}

?>

When the page is initially loaded, populate a JavaScript variable with a number from the database:

<script type="text/javascript">
var pageGenID = 25218603  //  generated by PHP
var processUpdate = function( response ) 
{
    if ( pageGenID < response ) 
    {
        replace_current_data_with_new_via_ajax();
        pageGenID = response;
    }
}
//  Compare our Page Generate ID against that of the server
var checkUpdates = function()
{
    serverPoll = setInterval( function()
    {
        $.get('script_to_return_latest_pageGenID.php', 
          { lastupdate: 1 }, 
          processUpdate, 'html');
    }, 10000 )
};

//  Check for updates every 10 seconds
$( document ).ready( checkUpdates );

</script>
Friday, December 23, 2022
1

If you make your db connection a Singleton class and you have an __autoload() function, then you can just have a dbconnection property in each of your classes which is set during construction.

e.g.

class DBConnection{

  private static $instance;

  private function __construct(){
    //make your db connnection
  }

  public static function get_connection(){
    if(empty self::$instance) self::$instance = new DBConnection;
    return self::$instance;
  }
}

class Foo {

  private $db;

  function __construct(){
    $this->db=DBConnection::get_connection();
  }
}
Monday, October 17, 2022
1

Use location.reload():

$('#something').click(function() {
    location.reload();
});

The reload() function takes an optional parameter that can be set to true to force a reload from the server rather than the cache. The parameter defaults to false, so by default the page may reload from the browser's cache.

Friday, October 7, 2022
 
eelco
 
2

It turns out that, because the URL we're using sets a cookie, and Hootsuite creates a frame, that IE won't trust the third party cookie (our site). So I've added some framebuster code to the page, and triggered it to happen "immediately" if the browser is IE. Code is below:

<meta http-equiv="refresh" content="15;url=<?php echo $url ?>" />
<script type="text/javascript">
var timeout = 1;
if (navigator.userAgent.match(/MSIE/)) { timeout = 1; } else { timeout = 14500; }
setTimeout('if (top != self) top.location.replace(self.location.href)', timeout);
</script>

Maybe this will help some other random user out there some day.

Sunday, September 25, 2022
 
rf43
 
5

You should probably override your onResume() to check if the list of alarms have been refreshed so that whenever you come to/come back to your ListActivity, it'll get updated.

Saturday, November 26, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :