0

I currently have a script that looks something like this:

<?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;
?>

The thing is that the variable $total will update often with a brand new number. I want the number to automatically update on the page, without the user having to refresh. I think that I might need to use AJAX by the looks of it, but my AJAX is very weak. Can anybody help me? Thanks!

2 Answers 2

1

Quick and dirty:

<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>
Sign up to request clarification or add additional context in comments.

Comments

1

You would have to use AJAX or simply use a meta- or javascript-refresh. Of coruse those refreshes would reload the page, but could do so without user interaction.

2 Comments

Ahh I see. Can you post an example of an AJAX sxript that could do this?
@user1930449 It would be best if you attempted the implementation and then can back and opened a question on SO if you run into any problems. So is not really a "write my code for me" site. There are hundreds of thousands of samples of using AJAX out there available for you to search from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.