我对输出此代码:如何在数量发生变化时实时输出?
$tot_clicks6 = $db->FetchArray($db->Query("SELECT SUM(visits) AS sum_visits FROM surf"));
和
<?=$tot_clicks6['sum_visits']?>
是显示总数。
我对输出此代码:如何在数量发生变化时实时输出?
$tot_clicks6 = $db->FetchArray($db->Query("SELECT SUM(visits) AS sum_visits FROM surf"));
和
<?=$tot_clicks6['sum_visits']?>
是显示总数。
你的问题代表了PHP的一个常见的误解。代码块
<?=$tot_clicks6['sum_visits']?>
只是你的服务器中的代码。当页面被加载时,它被作为HTML来使用,无论该变量的值是什么。例如,
6
为了实时更新你的页面,你需要使用AJAX 。
看到这个问题
或者你也可以使用一个实时的框架。我为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或类似的东西。您可以改为将数据推送到浏览器。
希望它有帮助!
这里没有javascript标记,但可以使用AJAX完成。 – 2013-05-13 14:41:48
以及我如何使用ajax? – 2013-05-13 14:43:00
你可以看看[jQuery](http://jQuery.com) – 2013-05-13 14:44:04