0

I am using a long poll script from https://github.com/panique/php-long-polling, which working well.

In this script they used a text file for get new data.

Question 1: How to use update.php instant data.txt in this server.php file to get data?

Question 2: Is my update.php file have right format to show data?

//I was tried this in server.php file but not show anything
$data_source_file = 'update.php';

server.php

set_time_limit(0);

$data_source_file = 'data.txt';

while (true) {

// if ajax request has send a timestamp, then $last_ajax_call = timestamp, else  $last_ajax_call = null
$last_ajax_call = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : null;

// PHP caches file data, like requesting the size of a file, by default.   clearstatcache() clears that cache
clearstatcache();

$last_change_in_data_file = filemtime($data_source_file);

if ($last_ajax_call == null || $last_change_in_data_file > $last_ajax_call) {

    // get content of data.txt
    $data = file_get_contents($data_source_file);

    // put data.txt's content and timestamp of last data.txt change into array
    $result = array(
        'data_from_file' => $data,
        'timestamp' => $last_change_in_data_file
    );

    $json = json_encode($result);
    echo $json;

    break;

    } else {
        // wait for 1 sec (not very sexy as this blocks the PHP/Apache process, but that's how it goes)
        sleep( 1 );
        continue;
    }
}

update.php

$u = mysqli_query($dbh,"SELECT * FROM updateside WHERE `parent_id`='".$parent."' AND `created` > '".$timestamp."' ORDER BY created DESC") or die(mysqli_error($dbh));
while ($row = mysqli_fetch_array($u)) {
$parent_id = $row['parent_id'];
$sub = $row['sub'];
$detail = $row['detail'];

echo '<div class="upbox" id="'.$parent_id.'">
// All result of above query
Subject:'.$sub.' <br>
Detail: '.$detail.'
</div>';
}

Clint.js

    function getContent(timestamp)
{
    var queryString = {'timestamp' : timestamp};

    $.ajax(
        {
        type: 'GET',
        url: 'http://myweb.com/server/server.php',
        data: queryString,
        success: function(data){
            // put result data into "obj"
            var obj = jQuery.parseJSON(data);
            // put the data_from_file into #response
            $('#response').html(obj.data_from_file);
            // call the function again, this time with the timestamp we just got from server.php
            getContent(obj.timestamp);
            }
        }
    );
}

// initialize jQuery
$(function() {
getContent();
});
joy
  • 81
  • 9
  • Please contact the vendor of the software you're using for your support options. – hakre Dec 26 '14 at 15:51
  • Tried to contact but cannot get any support so try to here also for expert support. – joy Dec 26 '14 at 15:55
  • Stackoverflow is not a support site. It's about creating a programming Q&A. If you're looking for expert advice here, you have to turn your question into a programming question. Having to ask two question at once most often is a sign that your question is too broad. Self-help is additionally available via the help center http://stackoverflow.com/help including tips on how to create the perfect question (if you're interested in high quality answers, write a good question. ping me if you have any further questions) – hakre Dec 26 '14 at 15:57
  • Sorry sir. I don't know about this. – joy Dec 26 '14 at 15:59
  • 1
    No problem. Take for example your second question: *"Is my update.php file have right format to show data?"* <- this is pretty subjective (so we can't answer it actually for a Q&A). But if you would say how the right format would construe from the specs (and which ones), then this *could* be answered. – hakre Dec 26 '14 at 16:00

0 Answers0