Echo new row when it got added to database - php

I am trying for a few days now to figure out how I can do this.
I have two pages, one is index.php, other is add.php .
1)Index.php - I want it do display last 5 rows from database table.
2)Add.php - contains form which adds new row to database.
How can I update values that are shown on index.php when I add new row?
So lets say I have index.php already opened on one tab, and on other one is Add.php. When I fill the form on add.php,
I want it to automatically notifies index.php that there is a new row,and that it should change values without refreshing the index.php tab.
Do I need VPS for this?What is the best way to do this theoretically?

There is no need in setting up a VPS for this. A VPS is Just a server in which u can install your own software. You are already using a server for PHP (local perhaps).
U can create a websocket server. Once you add a row you send a message tot the websocket server which will then tell the index page to resfresh.
Previous option is a lot of work for a rather simple functionaliteit. I'd suggest the simplest option as recommended by FrankerZ. Only if you refresh the page at a set interval using JavaScript (window.setInterval) then the page would keep refreshing. I don' t think that's ideal. You van however send a xhtmlhttprequest to store the contents for the index page and run that request every x seconds. Then u can compare the 2 contents and decide if the page should be refreshed or not.

Related

detect changes in database without reloading the page

I create a chat webpage.
by ajax I send data to database but for showing new data to both user in chat I have to detect last changes in database.
for this problem (detect last changes without reloading page) I create a loop for each 5 sec to connect to the data base and if there was a change, return that.
but i think its not functional.
any body knows what is the correct way to solve this?

Does javascript autorefresh on webpage create extra load on server?

I have implemented auto refresh function on a webpage which refresh page after 2 second. Initially I wanted to do this using shall script which will hit that url in every 2 second. but server administrator advised me that it is not good to hit a page in every 2 second because it create extra load to server. Now I want to know if autorefresh also create the extra load to server same as shall script.
Thanks

What is the best way to update the webpage only when a new entry is saved in the database?

I am wondering what will be the best way to update the webpage only when a new entry is saved in the database?
For example, suppose one of my visitor is reading a post on my blog. At the same time some other visitor comments on that particular blog post. Then I want that latest comment to appear on that page without reloading the page.
I know the technology used here will be AJAX/jQuery but will I have to run the AJAX continuously every time using something setTimeout()?
If I do so, then it will generate a lot of http request endlessly. Is it possible that AJAX runs only when it is needed indeed and not all the time?

WordPress - Update Database Value in real time.

I have a piece of code that counts the lines of code within a wordpress site. I have successfully managed to send that number to the wordpress database so I can call on it in a different location in my theme files (this is because the script needs to be in the root of the install to count the lines of code).
Whilst this works really nicely, the BIG issue I have is that the number won't update itself automatically. i.e. the code for counting lines is www.mysite.com/loc.php and I mus go to this page and let it load before it updates the value in the database.
Is there a way to make it so that the value just automatically updates, so I don't have to navigate to the page for this to happen? i.e. when I add more lines of code, the value in the database updates itself.
Code for sending value to the db is as follows:
$num_of_lines = $folder -> count_lines();
update_option('line_count', $num_of_lines);
look into jquery and ajax call
Wordpress provides for you the framework to call backend functions on the frontend
http://wptheming.com/2013/07/simple-ajax-example/
another example:
http://premium.wpmudev.org/blog/how-to-use-ajax-with-php-on-your-wp-site-without-a-plugin/
You need to create a trigger, in order to update automatically MYSQL database
Look at this, i hope it will help
How to automatically update a MYSQL column value using formula and data from other columns in same row?

Reloading Page Data after PHP Submit

ok folks,
I have created a PHP page that is querying a database, and through a whileloop, displays the contents of that database table with a REMOVE and PUSH button. The REMOVE button removes it from the database entirely, and the PUSH button pushes that entry into another database and sets a variable that the entry has been pushed.
What I'm running into is that I can't quite get the page to refresh, in turn running an new query of the first database and displaying only those entries that have not been removed or pushed.
I can only get the query to run correctly if I manually refresh the page, whether it be F5 or control+r (command+r).
What is the proper way to refresh the page so that the query will run again on page load?
If you want to reload the page using Javascript, try this:
window.location.reload(true);
You can also see this answer:
How to reload a page using JavaScript?
there are two ways
If putting extra load on db is not a problem, use jquery methods likes $.get()
$.get('url',{},function(data){
//load results in appropriate div;
});
If you don't want to put any extra load on database just hide the row when it is removed or pushed.
$('.remove').click(function{
$(this).css('display','none');
});
similarly make it for pushed
Do you have some extreme caching setup on your web hosting solution?
If maintaining nice-looking URLs on this page is a non-issue you could always set a timestamp in PHP and append it to the string.
I'm not big on PHP but a javascript example would look something like this.
ts = new Date();
urltorefresh += '?timestamp=' + ts.getTime();
location.href = urltorefresh;
This would make sure the page is absolutely not in the browser cache since this specific URL have never been requested before.

Categories