How to output in realtime when number is change?
Asked Answered
A

2

1

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.

Astto answered 13/5, 2013 at 14:37 Comment(5)
There is no javascript tag here, but it can be done with AJAX.Ripley
and how i make with ajax ?Astto
You could take a look at jQueryRipley
@mp3alesemp3alese: By working on your code, reading a couple of articles and using JavaScript.Ageless
i traying code: <?php $advert = array( 'advert' => $tot_clicks6['sum_visits'], ); echo json_encode($advert); ?> and show {"advert":"2"}Astto
B
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

Bushranger answered 13/5, 2013 at 14:42 Comment(0)
D
1

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!

Deductible answered 13/5, 2013 at 21:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.