1

I've a twitter streaming data in mysql and it keep adding every 5-10 record/second. Now, I want to get every new records that have created time start when client open the page and continuously, until the page is close. The data is json that contain geo-location for marking a map and it must be inside a javascript variable, here is json format:

{max data: 30; data: [{"lat":-6.92015,"lon":107.67024,"value":0.1},...]}

I've been digging into AJAX, but not success. Here my current code:

get_query.php

<?php
require_once('./db_connect.php');
$dbcon=new db;

$query="SELECT geo_lat, geo_long FROM location WHEN created_at >= NOW()";

$result = mysqli_query($dbcon, $query);
$data= array(); 

while($row= mysqli_fetch_assoc($result)){
    $data[] = array("lat"=>(float)$row["geo_lat"], "lon"=>(float)$row["geo_long"], "value"=>0.1);
}

echo json_encode($data);
?>

data.js

var data = new array();

$(function() {
setInterval(function() {
    $.ajax({
        type: "GET",
        url: "/absolute/path/of/get_query.php",
        dataType: "json",
        data: $(this).serialize(),
        success: function(response) {
           data = response;
       }
    });
}, 1000);
});

My question: 1. How to run my php code continuously after user open the page? 2. How to get data from php continuously without remove previous data with ajax? 3. It possibly to get event when user close the page to empty a record in mysql?

Thanks

y45
  • 47
  • 3
  • 9
  • you can use this display frame work for tweets display [140dev](http://140dev.com/free-twitter-api-source-code-library/twitter-display/download/). **Note** It run on its database structure that comes with [Twitter database server](http://140dev.com/free-twitter-api-source-code-library/twitter-database-server/download/) – Sohail Ahmed May 28 '13 at 06:56
  • Thanks, I've tried it, but ajax button not work at the end of the page, I'll try to figure it out. – y45 May 28 '13 at 09:56

1 Answers1

0

For 1 and 2, you need to use polling. Check this example: How do I implement basic "Long Polling"?

For 3, you can use this:

window.onbeforeunload = function() {
    $.ajax({
       type: 'POST',
       async: false, //Important
       url: 'function.php', //Here goes the function that deletes rows
       data: {param: param} //Here the params, if needed
    });
 };

I thin that should solve this problem!

Community
  • 1
  • 1
luqita
  • 4,008
  • 13
  • 60
  • 93
  • Thanks, it seems need hard way to learn it. By the way, I tested to print js variable above with `document.getElementById("id").innerHTML=data;` into html `div id` but nothing to appear, although my php code is working correctly. My json data format is `[{"lat":-6.92015,"lon":107.67024,"value":0.1},...]`, any advice? – y45 May 28 '13 at 01:26