reflect the list without refreshing the page in php - php

I am trying to show the list of online users in my application. Let me explain my requirement.
I have a Mysql DB table where list of username and their status mode(either 1 or 0) are storing. I have two php pages. One is for listing down all user's name and the status mode. Second page is for editing the mode of users from 1 to 0 and vice versa.
Now I open these two pages from different system. If I change the status of one user(edit page) from one system then automatically it will reflect to the another system, where the listing page is opened, with the updated record and obviously this should happen before refresh the listing page. The same like gtalk chat users.
I am not asking the code, but please help me how to proceed to resolve the issue. Obviously, cronjob is one of the solutions, please provide another solution.
Thanks in advance.

Well cronjobs are in fact not the thing you need.
With cronjobs you can schedule a task. What you want is client side refresh when new info is found. While cronjobs are server side and always on an interval.
What you need is polling or commet
The first, polling, you use your client side to execute a script every x seconds and look if there is new info (waste of resources in my opinion).
Commet, is now a days a better solution. But often hard to implement. I used pusher for this type of stuff. You can push messages to (all) clients connected and say there is new info. And then they will update or with the message comes also the new info

To achieve something like this, you should use JavaScript and Ajax in the clientside.
Give the XMLHttpRequest a try. To make it easier you could use something like jQuery.
On the serverside you could use json to transmit the data.
Read the data from the table and put it into an array, let's call it users, the keys are the names and the values are their mode(1 or 0).
Then use json_encode(ARRAY):
//Echo the results in json format
echo(json_encode($users));
Let's say, the users 'Frank', 'Susan' and 'George' are online and 'Isabell' and 'John' are offline. Then the script would result in an output similar to this:
{"Frank":1,"Susan":1,"George":1,"Isabell":0,"John":0}
Of course you need to put this and the loading into another php-script, maybe refresh.php.
And, to read the data from the script, add some JavaScript to your view page.
Use the XMLHttpRequest to request data from the script you just added.
Or, if you use jQuery, you can simply use $.getJSON("NameOfTheScriptYouJustWrote") which returns an already parsed object.
Then use the returned data to update the list of users. And refresh it every 5-20 seconds.
And keep in mind that this is not an efficient way at all and that this will not work well if there are many clients using your service.

Related

How to track Update in database table using PHP?

I want to ask is there a way to track database table using php mysql.
I want to do something like i have a table called post. Now when user post some data other user need to view this data. That is latest one need to be view to user on the top. We can do this by refreshing div after every few sec or using ajax. But can we use Trigger. As we know it automatically fires when something is executed. Hence i want to know can we use trigger in PHP code to automatically detect changes in table. And when a new post is available it needs to return the data from database. Please give me a brief description about this. Thank you in advance.
The trigger is executed on Mysql Server, not on the PHP one (even if those are both on the same machine).
So, I would say this is not quite possible -- at least not simply.
Still, considering this entry from the MySQL FAQ on Triggers :
23.5.11: Can triggers call an external application through a UDF?
Yes. For example, a trigger could invoke the sys_exec() UDF available at MySQL Forge here: http://forge.mysql.com/projects/project.php?id=211
So, there might be a waty, actually, via an UDF function that would launch the php executable/script ; not that easy, but seems possible ;-)
Read more about it in:
https://stackoverflow.com/a/1467387/3653989
SQL trigger is a database object executed server-side.
You want a front-end technique to refresh your data without refreshing the whole page.
You can refresh your page using:
<meta http-equiv="refresh" content="5">
With PHP, you can refresh the page using:
header("refresh: 3;");
but no-one would suggest you to use such a method, because your need is refreshing the page, only after a change in your database, and not continuously.
So, if you already use PHP, you need Javascript Push technology:
Push, or server push, describes a style of Internet-based communication where the request for a given transaction is initiated by the publisher or central server. (wikipedia)
JavaScript polling, long-polling, real-time techniques, and javascript frameworks such as jquery, node.js, socket.io include a lot of practices that give you this possibility.

Instant web notification

Straight to my issue, I have a database which has casino tables; each table has some places, where I can add people and remove them.
All this works fine, but when I open my project on two different browsers, I cant see the updates which I've done from the other one.
So I was thinking for AJAX request on every 5sec or something like this, but I don't like this approach.
Then I started to look for another solution and found this MQTT server, but couldn't find a good example how it works with MySQL. I saw that Mosquito-PHP library, and maybe I can get it works on my server, but I'm confused How to get the status. If someone add a person to a table. How Do I check, there is a change?
I've red that the MQTT use something as infinity loop is it good idea to check in MySQL for changes in this loop?
Thank you in advance for any suggestions; and, sorry for my English, still learning.
I believe you need to divide your complicated task into simpler parts, possibly these could be a guideline:
for each browser session you should have a last update date
whenever the browser extracts relevant data, your session's update date should be updated
you should have a last event date on the server
you should send ajax request every five seconds to the server, called heartbeat event
upon each heartbeat, the server should check whether your last update is earlier than the last event and send a response in this vein
if your ajax request yields the result that your status is not at least as new as the last update, the client-side should send another request for new info

Refresh a page after data in MySQL database changes

Good day all,
Basically what's I've got is a PHP based site tied to a MySQL database all on a local web server (nothing is being accessed from outside of the company). The index page displays an image resembling a bar chart. Employees of the company will be entering data periodically which will update the image that appears on the index page. The index page will be displayed on a couple different screens throughout the company and I need that index page to refresh after someone alters the data in the database.
I've been messing around with various AJAX solutions, but as I don't know much about AJAX I'm having trouble adapting something to work the way I need. Here's the way I've was thinking about:
-- on the index.php run a JavaScript function every minute or so that gets a response from dataChanged.php
-- dataChanged.php will query the database and get a timestamp from one of the tables.
-- the script on the index.php will then compare the timestamp to the last time the page was refreshed (or some variable that stores such information) and refresh if the data is new.
I'm somewhat proficient in PHP, but am very limited with JavaScript (and thus AJAX).
Can someone get me pointed in the right direction?
Thanks!
What you want it the standard javascript function setInterval
Have it execute a ajax call every now and then to get new data. Try to get a standars librabry that knows this stuff, probably jQuery
Depending on if the chart generation is time consuming or not I would go with different strategies. The preferred way would be to just generate the graph on each call, but if that is very time consuming I think your two-step solution works great.
Check out the link http://blog.codebusters.pl/en/auto-refresh-content-after-changes-in-database-ajax.
Here you can see how to refresh your page after change in db.

Making a menu of tables in php

Ok what I am trying to make is a system that supports tickets. Tickets have all kinds of info on a specific job. How can I make a ticket menu with like links to tickets which contain all the info I need. For instance I click on ticket number 777 so it I have the php?id=777 in the url.
I need this page to constantly look for new tickets.
what you need to do is add a the correct ticket to an anchor
Ticket 777
Ticket 778
on the page.php you can access the ticket number using the $_GET variable
<?php
$ticket = $_GET['id'];
/// now you have the ticket number in the variable
?>
I need this page to constantly look for new tickets.
I'm not 100% sure what you're exactly looking for here, but there are two basic solutions here:
AJAX to update the content without reloading the page
Reload the page every few minutes and have PHP handle it
The first is better for things that are constantly updated, like Twitter posts, news, stock-tickers, etc, but it's a little overkill for something that updates relatively rarely (takes longer than 6 minutes or so).
The tickets should be stored in some kind of database and then read out and looped over to create the table. This would probably make more sense in a list format (stacked divs) instead of a table, but then again, I don't know the specifics.
For the simple PHP generation of links, #Ibu has a good example.
EDIT:
For more information about implementing AJAX, this page has a good example. I would recommend using a framework like jQuery or MooTools to handle the AJAX because there are some inconsistencies between browsers.
EDIT:
From the comments you made, it sounds like you are not very familiar with how PHP works.
PHP is just a templating language with some programming language features. It is best used to generate pages on the fly with dynamic content from a database.
When you try to request a .php page, you are actually telling the server to execute the code in that file. When all the code is finished, the resulting document is given to the requesting browser. The result should be valid HTML if done correctly.

The best way to access data from a database every X seconds (asynchronously)

Ok, I didn't really now how to formulate this question, and especially not the title. But i'll give it a try and hope i'm being specific enough while trying to keep it relevant to others.
I you want to run a php script in the background (via ajax) every X seconds that returns data from a database, how do you do this the best way without using to much of the server resources?
My solution looks like this:
A user visits a webpage, ever x seconds that page runs a javascript. The javascript calls a PHP script/file that calls the database, retrieves the data and returns the data to the javascript. The javascript then prints the data to the page. My fear is that this way of solving it will put a lot of pressure on the server if there is a lot (10 000) simultaneous visitors on the page. Is there another way to do this?
That sounds like the best way, given the spec/requirement you set out.
Another way is to have an intermediary step. If you are going to have a huge amount of traffic (otherwise this does not introduce any benefit, but to the contrary may overcomplicat/slow the process), add another table that records the last time a dataset was pulled, and a hard file (say, XML) which if the 'last time' was deemed too long ago, is created from a new query, this XML then feeds the result returned to the user.
So:
1.Javascript calls PHP script (AJAX)
2.PHP pings DB table which contains last time data was fully output
3.If time is too great, 'main' query is rerun and XML file is regenerated from output
ELSE skip to 4
4.Fetch the XML file and output as appropriate for returned AJAX
You can do it the other way, contacting the client just when you need it and wasting less resources.
Comet it's the way to go for this option:
Comet is a programming technique that
enables web servers to send data to
the client without having any need for
the client to request it. This
technique will produce more responsive
applications than classic AJAX. In
classic AJAX applications, web browser
(client) cannot be notified in real
time that the server data model has
changed. The user must create a
request (for example by clicking on a
link) or a periodic AJAX request must
happen in order to get new data fro
the server.

Categories