Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中自动更新变量输出?_Php_Ajax_Live - Fatal编程技术网

如何在PHP中自动更新变量输出?

如何在PHP中自动更新变量输出?,php,ajax,live,Php,Ajax,Live,我目前有一个脚本,看起来像这样: <?php $pattern = '/(?<=\=\s)([0-9]+)(?=\s\=)/'; $total = 0; $matches; $handle = @fopen("log.txt", "r"); if ($handle) { while (($buffer = fgets($handle, 4096)) !== false) { if(preg_match($pattern, $buffer,

我目前有一个脚本,看起来像这样:

<?php
$pattern = '/(?<=\=\s)([0-9]+)(?=\s\=)/';
$total = 0;
$matches;

$handle = @fopen("log.txt", "r");

if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {        
        if(preg_match($pattern, $buffer, $matches))
        {
            $total += intval($matches[0]);
        }       
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}
echo $total;
?>


问题是变量$total将经常更新为一个全新的数字。我希望号码在页面上自动更新,而用户无需刷新。从外观上看,我认为我可能需要使用AJAX,但我的AJAX非常弱。有人能帮我吗?谢谢

您必须使用AJAX,或者只需使用meta或javascript刷新。当然,这些刷新会重新加载页面,但可以在没有用户交互的情况下重新加载。

快速脏:

<html lang="en">
    <head>
        <script type="text/javascript" src="http://www.google.com/jsapi"></script>
        <script type="text/javascript">
            google.load('jquery', '1.4.2'); 
            google.load('jqueryui', '1.8.8');
        </script>
        <script type="text/javascript">
            $(document).ready(function() {
                setInterval('get_counter()', 500);
            });
            function get_counter()
            {
                $('.counter').load('PATH_TO_YOUR_PHP_SCRIPT');
            }
        </script>
    </head>
    <body>
        <h1>Counter</h1>
        <div class="counter"></div>
    </body>
</html>

load('jquery','1.4.2');
load('jqueryui','1.8.8');
$(文档).ready(函数(){
setInterval('get_counter()',500);
});
函数get_counter()
{
$('.counter').load('PATH_TO_YOUR_PHP_SCRIPT');
}
柜台

啊,我明白了。你能发布一个AJAX SXcript的例子来做这件事吗?@user1930449如果你尝试实现,然后可以返回并打开一个问题,那么如果你遇到任何问题,那将是最好的。所以这不是一个真正的“为我编写代码”的网站。有成千上万的使用AJAX的示例可供您搜索。