views:

25

answers:

1

So I have an external script that will sometimes input something into a sql database throughout the day (usually about 3 times a day, but could potentially update ever minute) I need to find a way to make the html page (that displays the sql entries) refresh automatically when there is a new entry that's added. Any suggestions for PHP and SQL? I was thinking Ajax, but I'm pretty sure I will need to do a full page refresh, because it will update images too.

A: 

You need a way to let the browser know that the data has been updated.

The easiest way would be to have an ajax call that pools the server to check if new data has been added. You can use javascript to set this to be called every minute or however often you want. Once the ajax call shows that new data has been loaded, then you can use javascript to refresh the page.

This approach will mean you have a lot of calls to your server, but they will be small and the full data will only be loaded when needed.

Alan Geleynse
you could even create a simple file with a timestamp and let the ajax call compare a locally stored value to the value in the file to determine whether to hit the db or not.
thetaiko
@thetaiko Exactly, I didn't go into the implementation of the backend, but there are many simple ways to do it like the one you mention. The main problem here is getting that back to the browser, and an ajax call is the simplest method for that.
Alan Geleynse
Cool, thanks... I'm pretty new to JavaScript and AJAX, so I'm looking for a good tutorial (If anyone knows of one that'd be great). Pretty much I just need to figure out how to check for a new entry and then have a conditional statement to say what happens if there is a new entry, I'm sure I can figure out the rest. I want it to make a call to the server every min. to check for a new entry.
Phil
Checking for a new entry will need to be done on the server side. You can do what thetaiko said and have a trigger that updates a timestamp in a file when there is new data, or you can have a PHP script that queries your database for the most recently modified data. Either way, you just need a server side PHP script that determines the result, and then it is a simple AJAX call that can check the result of that script.
Alan Geleynse
Hmm, ok. I guess I can use the PHP script that writes the data into the database, and the AJAX can just check that script to see if it's the same as the previous call, and if it's not then refresh.
Phil