Is it possible to refresh a divs content only if new content is added to the database?
I’m using this for “recent posts” that appear in the side menu.
Right now the div is set refresh every 10 seconds.
Is it somehow possible to check it a new post was added to the db and then add only that posts data to the div?
I’m using MySql, php and jquery to do all of this.
YES IT IS POSSIBLE
The below code will get the recent photos that has been added:
This can be used to create a live blog for example, where the user will see all the recent comments, images or posts, or everything, even if he doesn’t reload the current page. And it won’t consume a lot of bandwidth as the content will be reloaded ONLY IF THERE IS NEW ADDED (it will only send a tiny check in the database otherwise).
I’ve just finished working out a solution for that, and I have to share it with you. I was doing it for WordPress, i needed a function that gets the photos that has been added ONLY after the user loaded the page. It could be done by simply refreshing the div every 5 seconds, but imagine if there were 20 photos, and they had to be refreshed every 5 seconds… that’s a lot of MB of data. So we will refresh them ONLY when a new photo is added (you can apply it to anything, from posts to comments or users, etc.).
There are 3 PHP files: timer.php, check.php and display.php.
The timer.php will load the check.php every 5 seconds, to check if new content is added. Notice the -6 extraction from the time of the current load of check.php.
The date-time of the timer.php will be passed (by check.php?tim=**print date) through the check.php (by **display.php?timm=’. $tizz .’) and to the display.php, so we can use it for a reference in our database (that will be the time from where we will load all the images, if new images are added).
If any questions, just ask. It has to work for you too, as it works for me.
ENJOY! 🙂
Below are the 3 PHP files, just customize them for your needs:
THE TIMER.PHP (or your wordpress blog pages):
THE CHECK.PHP :
THE DISPLAY.PHP: