问题
I want to use php-long polling in my php script to get the instant display of data when admin changes anything from backend.
I have browsed for solutions, But could not find any.
I have downloaded php-long-polling- master from github. There , i have a file server.php.
There the code is like this
<?php
/**
* Server-side file.
* This file is an infinitive loop. Seriously.
* It gets the file data.txt's last-changed timestamp, checks if this is larger than the timestamp of the
* AJAX-submitted timestamp (time of last ajax request), and if so, it sends back a JSON with the data from
* data.txt (and a timestamp). If not, it waits for one seconds and then start the next while step.
*
* Note: This returns a JSON, containing the content of data.txt and the timestamp of the last data.txt change.
* This timestamp is used by the client's JavaScript for the next request, so THIS server-side script here only
* serves new content after the last file change. Sounds weird, but try it out, you'll get into it really fast!
*/
// set php runtime to unlimited
set_time_limit(0);
// where does the data come from ? In real world this would be a SQL query or something
$data_source_file = 'data.txt';
// main loop
while (true) {
// if ajax request has send a timestamp, then $last_ajax_call = timestamp, else $last_ajax_call = null
$last_ajax_call = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : null;
// PHP caches file data, like requesting the size of a file, by default. clearstatcache() clears that cache
clearstatcache();
// get timestamp of when file has been changed the last time
$last_change_in_data_file = filemtime($data_source_file);
// if no timestamp delivered via ajax or data.txt has been changed SINCE last ajax timestamp
if ($last_ajax_call == null || $last_change_in_data_file > $last_ajax_call) {
// get content of data.txt
$data = file_get_contents($data_source_file);
// put data.txt's content and timestamp of last data.txt change into array
$result = array(
'data_from_file' => $data,
'timestamp' => $last_change_in_data_file
);
// encode to JSON, render the result (for AJAX)
$json = json_encode($result);
echo $json;
// leave this loop step
break;
} else {
// wait for 1 sec (not very sexy as this blocks the PHP/Apache process, but that's how it goes)
sleep( 1 );
continue;
}
}
If anything we change in data.txt, it displays as and when in real time. But i am not getting how and where i can add the query or how do i include it in my file.
in server.php, it is there like
// where does the data come from ? In real world this would be a SQL query or something
$data_source_file = 'data.txt';
but where and how can i add sql query?
can somebody help me how can i start with or how can i add sql query
**** My CODE *****
After many attempts i could do it like this
in server.php, i did like this
<?php
/**
* Server-side file.
* This file is an infinitive loop. Seriously.
* It gets the file data.txt's last-changed timestamp, checks if this is larger than the timestamp of the
* AJAX-submitted timestamp (time of last ajax request), and if so, it sends back a JSON with the data from
* data.txt (and a timestamp). If not, it waits for one seconds and then start the next while step.
*
* Note: This returns a JSON, containing the content of data.txt and the timestamp of the last data.txt change.
* This timestamp is used by the client's JavaScript for the next request, so THIS server-side script here only
* serves new content after the last file change. Sounds weird, but try it out, you'll get into it really fast!
*/
// set php runtime to unlimited
set_time_limit(0);
// where does the data come from ? In real world this would be a SQL query or something
$data_source_file = 'data.txt';
$servername = "localhost";
$username = "*****";
$password = "******";
$dbname = "mydb";
// Create connection
$conn = mysqli_connect($servername, $username, $password , $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// main loop
while (true) {
// if ajax request has send a timestamp, then $last_ajax_call = timestamp, else $last_ajax_call = null
$last_ajax_call = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : null;
// PHP caches file data, like requesting the size of a file, by default. clearstatcache() clears that cache
// clearstatcache();
// get timestamp of when file has been changed the last time
// $last_change_in_data_file = filemtime($data_source_file);
// Create database
$sql = "SELECT max(tax_id) FROM taxes";
$result = mysqli_query($conn, $sql);
$last_change_in_data_file = mysqli_fetch_array($result, MYSQLI_NUM)[0];
// if no timestamp delivered via ajax or data.txt has been changed SINCE last ajax timestamp
if ($last_ajax_call == null || $last_change_in_data_file > $last_ajax_call) {
// get content of data.txt
// $data = file_get_contents($data_source_file);
// Create database
$sql = "SELECT tax_id, name FROM taxes";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$data .= "id: " . $row["tax_id"]. " - Name: " . $row["name"]. "<br>";
$last_change_in_data_file = $row["id"];
}
}
// mysqli_close($conn);
// put data.txt's content and timestamp of last data.txt change into array
$result = array(
'data_from_file' => $data,
'timestamp' => $last_change_in_data_file
);
mysqli_close($conn);
// encode to JSON, render the result (for AJAX)
$json = json_encode($result);
echo $json;
// leave this loop step
break;
} else {
// wait for 1 sec (not very sexy as this blocks the PHP/Apache process, but that's how it goes)
sleep( 1 );
continue;
}
}
But i want to get the data according to particular id. my sql statement has to be something like this
$sql = "SELECT tax_id, name FROM taxes WHERE grp_id=".$id."";
So i am not getting from where i should pass the id to this page
Can somebody help me in this??
My scenario:
I am trying to display the data in realtime. my taxes table contains a column is_ok. When admin press a button OK for particular group, taxes column gets updated as is_okay=1 for that group. All the online users of that group gets the notification that the admin has hit the button and waiting for you approval. Then the users will approve that .....
so for this i am trying to pass the grp_id to taxes table from admin's page. But after hitting the 'OK' button admin should get the immediate approval notification from all users who are all done that in the same page. so there i am facing problem
来源:https://stackoverflow.com/questions/47453281/implementing-php-long-polling