1

want to retrieve a records data from Mysql and store it into Javascript array for heatmap.js map data in this format :

var testData = {max: 46, data: [{lat: 33.5363, lon:-117.044, value: 1},{lat: 33.5608, lon:-117.24, value: 1},..]};

Now I get stuck at here, and I don't know how to connecting from Jquery into my var testData = new Array();, How I should do to solve this?

(UPDATED CORRECT CODE)

get_query.php

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


$query="SELECT (SELECT geo_lat FROM fun WHERE created_at <= DATE_SUB(NOW(), interval 1   minute) AS geo_lat," . 
       "(SELECT geo_long FROM fun WHERE created_at <= DATE_SUB(NOW(), interval 1 minute) AS geo_long";

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

    while($row= mysqli_fetch_assoc($result)){

     $data[] = array("lat"=>$row["geo_lat"], "lon"=>$row["geo_long"], "value"=>1); 
     $post_data = json_encode(array('max' => 46, 'data' => $data));
    }
    echo $post_data;
 ?>

my_data.js based from here:

jQuery.extend({
getValues: function(url) {
    var result = null;
    $.ajax({
        url: url,
        type: 'get',
        dataType: 'json',
        async: false,
        success: function(data) {
            result = JSON.stringify(data);
        }
    });
   return result;
}
});

var testData = $.getValues("get_query.php");

Thanks to Orangepill and Chrislondon.

6
  • 4
    whoot while(true){ ? Commented May 21, 2013 at 15:10
  • 1
    Why do you have while(true){?
    – gen_Eric
    Commented May 21, 2013 at 15:14
  • json_encode should only be called once, at the very end, once the array is built the way you want it.
    – gen_Eric
    Commented May 21, 2013 at 15:15
  • Are you trying to create a continuous feed of data?
    – Reactgular
    Commented May 21, 2013 at 15:15
  • sorry that wrong line, I was updated now.
    – y45
    Commented May 21, 2013 at 15:15

3 Answers 3

3
while($row= mysqli_fetch_assoc($dbcon,$result)){
    $data[] = array("lat"=>$row["geo_lat"], "lon"=>$row["geo_long"], "value"=>1);
}
echo json_encode($data);

should get you what you are looking for. when building data for json_encode just use native php types instead of json, let json_encode take care of that

2
  • 1
    Thanks, when I executed the php script, the page result this '[]', are this correct? sorry for my lack of knowledges.
    – y45
    Commented May 21, 2013 at 18:10
  • When I added 'echo $data;' at the end of code, I got 'PHP Warning: mysqli_query() expects parameter 1' and 'PHP Warning: mysqli_fetch_assoc() expects exactly 1 parameter'.
    – y45
    Commented May 21, 2013 at 18:28
2

So your question is how to connect your jQuery to your var data so I won't get into the myriad of problems in your PHP code. In your success function you can set the var data like so:

var data = new Array();

$(function() {
    $.ajax({
        type:     "post",
        url:      "get_query.php",
        data:     $(this).serialize(),
        dataType: "json"
}).done(function(response) {
    data = response;
});
6
  • Hi Chris, how to tested this code, I has implemented with heatmap.js but it still not works.
    – y45
    Commented May 21, 2013 at 20:30
  • I haven't used heatmap.js before but looking at their code try doing this: heatmap.store.setDataSet(response); inside of the done function. Commented May 22, 2013 at 13:56
  • it seems the code not work because the var data resulting double quotes in json data {"lat":"-6.92015","lon":"107.67024","value":1}. how I can remove that?
    – y45
    Commented May 22, 2013 at 17:02
  • Hmm... maybe try this in your php: $data[] = array("lat"=>(float)$row["geo_lat"], "lon"=>(float)$row["geo_long"], "value"=>1) Commented May 22, 2013 at 17:35
  • Thanks, it work, but my json still don't store in javascript variable, it is need to be parse? I really confuse working with jquery-AJAX, I dont receive any data and error.
    – y45
    Commented May 28, 2013 at 11:55
0

Your json seems incorrect:

var testData = {max: 46, data: [{lat: 33.5363, lon:-117.044, value: 1},{lat: 33.5608, lon:-117.24, value: 1},..]};

should be with quotes around the keys

var testData = {"max": 46, "data": [{"lat": 33.5363, "lon":-117.044, "value": 1},{"lat": 33.5608, "lon":-117.24, "value": 1},..]};

Since you use json_encode() in php I would assume PHP outputs this correctly. But maybe you are for now just working with a fixed (faulty) string for testing purposes?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.