2013-05-13 69 views
0

我對輸出此代碼:如何在數量發生變化時實時輸出?

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

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

是顯示總數。

+0

這裏沒有javascript標記,但可以使用AJAX完成。 – 2013-05-13 14:41:48

+0

以及我如何使用ajax? – 2013-05-13 14:43:00

+0

你可以看看[jQuery](http://jQuery.com) – 2013-05-13 14:44:04

回答

1

你的問題代表了PHP的一個常見的誤解。代碼塊

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

只是你的服務器中的代碼。當頁面被加載時,它被作爲HTML來使用,無論該變量的值是什麼。例如,

6 

爲了實時更新你的頁面,你需要使用AJAX

看到這個問題

Get variable from PHP file using JQuery/AJAX

1

或者你也可以使用一個實時的框架。我爲Realtime.co工作,我們就是這麼做的。

您可以在www.realtime.co獲得免費許可證,獲取PHP API http://www.xrtml.org/downloads_62.html#pubsub:php,並使用以下代碼傳播信息頁面(例如管理頁面)。注意:這段代碼與您在Github上找到的適合您需求的ORTC示例(https://github.com/RTWWorld/pubsub-examples/tree/master/PHP)相同。

<?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/>'; 
}  

?> 

在接收方頁面上,您需要使用JavaScript接收數據並顯示它。對於這個例子,我只是用數據提醒用戶。

<!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> 

有了這段代碼,你將不需要使用AJAX或類似的東西。您可以改爲將數據推送到瀏覽器。

希望它有幫助!

相關問題