0

Basically, I'm trying to put together a live feed on my website for an external API that pushes all new data to my database,

Here's the current HTML code I'm using to grab my PHP file and get any new content added to the database

$min_id_result = $DB->select("SELECT `id` FROM `api_media` ORDER BY `id` DESC",true);
//Basically all this is doing is returning the latest ID in the database

var Id = "<?=$last_id_result['id'];?>";
function waitForPics() {
    $.ajax({
        type: "GET",
        url: "/ajax.php?id="+Id,

        async: true,
        cache: false,

        success: function(data){
            var json = eval('('+ data +')');
            var img = json['img'];
            if(json['img'] != "") {
                $('<li/>').html('<img src="'+img+'" />').appendTo('#container')
            }
            Id = json['id'];
            setTimeout(waitForPics,1000);
        },
     });
}

$(document).ready(function() {
    waitForPics();
 });

Here's the ajax.php file I'm using to process the database

$min_id_result = $DB->select("SELECT * FROM `api_media` ORDER BY `id` DESC",true);
$next_min_id = $min_id_result['id'];

$last_id = $_GET['id'];

while($next_min_id <= $last_id) {
    usleep(10000);
    clearstatcache();
}

$qry_result = $DB->select("SELECT * FROM `api_media` ORDER BY `id` DESC");

$response = array();
foreach($qry_result as $image) {
    $response['img'] = $image['image'];
    $response['id'] = $image['id'];
}
echo json_encode($response);

The problem I'm having is it's returning no data to me from the request, It appears all correct to me but obviously a second developers eye appears better at times

Curtis
  • 2,646
  • 6
  • 29
  • 53
  • 1
    you are never updating anything in your `while` loop so its never going to give you new data. You usually use a while loop with the `usleep` to check information every so often, in yours all you do is tell the stat cache to clear. – Patrick Evans Jul 10 '13 at 17:37
  • did you ever watch the network traffic (firebug, google chrome tools, ngrep, tcpdump...) weather you get any response at all? – Daniel W. Jul 10 '13 at 17:39
  • @patrick, that's all I want it to do, check both Ids against each other if they're the same then clear stat cache for CPU reasons etc.. if they're not it'll hop out the while loop and perform the rest of the file – Curtis Jul 10 '13 at 17:40
  • once your inside the loop, it doesn't break out (until script/request timeout) – Daniel W. Jul 10 '13 at 17:41
  • @Curtis, then all you need is an if statement, its always going to be checking the same values over and over, if ever $next_min_id is less then or equal to $last_id, its gonna loop forever and then timeout like DanFromGermany mentions. – Patrick Evans Jul 10 '13 at 17:42

1 Answers1

0

This should work:

while($next_min_id <= $last_id) {
    usleep(10000);
    clearstatcache();
    $min_id_result = $DB->select("SELECT * FROM `api_media` ORDER BY `id` DESC",true);
    $next_min_id = $min_id_result['id'];
}

inside the while you wait for a specific parameter to change, but you need to give it the chance to change.

The variables do not change themselves, you need to refresh them.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151