Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
4  
whoot while(true){ ? – NullPoiиteя May 21 at 15:10
1  
Why do you have while(true){? – Rocket Hazmat May 21 at 15:14
json_encode should only be called once, at the very end, once the array is built the way you want it. – Rocket Hazmat May 21 at 15:15
Are you trying to create a continuous feed of data? – Mathew Foscarini May 21 at 15:15
sorry that wrong line, I was updated now. – y45 May 21 at 15:15
show 1 more comment
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

share|improve this answer
Thanks, when I executed the php script, the page result this '[]', are this correct? sorry for my lack of knowledges. – y45 May 21 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 May 21 at 18:28
Hi, its work now. Thanks for your help – y45 May 21 at 18:52

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;
});
share|improve this answer
Thanks, I'll test it after I fix the php code. – y45 May 21 at 18:14
Hi Chris, how to tested this code, I has implemented with heatmap.js but it still not works. – y45 May 21 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. – chrislondon May 22 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 May 22 at 17:02
Hmm... maybe try this in your php: $data[] = array("lat"=>(float)$row["geo_lat"], "lon"=>(float)$row["geo_long"], "value"=>1) – chrislondon May 22 at 17:35
show 3 more comments

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?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.