How to output in realtime when number is change?

爷,独闯天下 提交于 2019-12-14 02:38:47

问题


I have this code for output :

$tot_clicks6 = $db->FetchArray($db->Query("SELECT SUM(visits) AS sum_visits FROM surf"));

and

<?=$tot_clicks6['sum_visits']?>

is display total of number.


回答1:


Your question represents a common misconception with PHP. The block of code

<?=$tot_clicks6['sum_visits']?>

Is only that code in your server. As the page is loaded, it gets served as HTML as whatever the value of that variable is. For example,

6

In order to update your page in real time, you need to use AJAX.

See this question

Get variable from PHP file using JQuery/AJAX




回答2:


Or you can use a real time framework. I work for Realtime.co and we do just that.

You can get a free license at www.realtime.co, get the PHP API at http://www.xrtml.org/downloads_62.html#pubsub:php and use the following code for the page that should broadcast the information (your administration page, for example). Note: this code is the same you can find at Github for the ORTC example (https://github.com/RTWWorld/pubsub-examples/tree/master/PHP) adapted for your needs.

<?php
error_reporting(E_ALL);
session_start();
require('./ortc.php');

/* -------------------- */
/* REPLACE THESE VALUES */
/* -------------------- */
$URL = 'http://ortc-developers.realtime.co/server/2.1';
$AK = 'YOUR_APPLICATION_KEY';// your realtime.co application key
$PK = 'YOUR_APPLICATION_PRIVATE_KEY';// your realtime.co private key
$TK = 'YOUR_AUTHENTICATION_TOKEN';// token: could be randomly generated in the session
$CH = 'MyChannel'; //channel
$ttl = 180; 
$isAuthRequired = false;
$result = false;
/* -------------------- */
/*        END           */
/* -------------------- */

// ORTC auth
// on a live usage we would already have the auth token authorized and stored in a php session
// Since a developer appkey does not require authentication the following code is optional

if( ! array_key_exists('ortc_token', $_SESSION) ){    
    $_SESSION['ortc_token'] = $TK;       
}   

$rt = new Realtime( $URL, $AK, $PK, $TK );  

    // Your query
    $tot_clicks6 = $db->FetchArray($db->Query("SELECT SUM(visits) AS sum_visits FROM surf"));

if($isAuthRequired){
    $result = $rt->auth(
        array(
            $CH => 'w'
        ), 
        $ttl
    );//post authentication permissions. w -> write; r -> read
    echo 'authentication status '.( $result ? 'success' : 'failed' ).'<br/>';
}

if($result || !$isAuthRequired){
    $result = $rt->send($CH, tot_clicks6['sum_visits'], $response);
    echo ' send status '.( $result ? 'success' : 'failed' ).'<br/>';
}    

?>

On the receiver page, you'll need to receive the data, using JavaScript and display it. For this example I'm just alerting the user with the data.

<!doctype html>
<html>
<head>
</head>
<body>

    <script src="http://code.xrtml.org/xrtml-3.2.0.js"></script>
    <script>
        var appkey = 'YOUR_APPLICATION_KEY';
        var url = 'http://ortc-developers.realtime.co/server/2.1';
        var authToken = 'YOUR_AUTHENTICATION_TOKEN';
        var channel = 'MyChannel';

        xRTML.load(function(){

            xRTML.Config.debug = true;

            xRTML.ConnectionManager.create({
                id: 'myConn',
                appkey: appkey,
                authToken: authToken,
                url: url,
                channels: [
                {name: channel}
                ]
            }).bind({
                message: function(e) {
                    alert(e);
                }
        });
        });
    </script>
</body>
</html>

With this code you won't need to use AJAX or anything like that. You'll be able to push your data to browsers instead.

Hope it helps!



来源:https://stackoverflow.com/questions/16524727/how-to-output-in-realtime-when-number-is-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!