Getting database values in Javascript variable dynamically

前端 未结 3 1201
长情又很酷
长情又很酷 2021-01-27 06:03

I researched this subject many times but I could not find the right answer to my question. Let me explain it.

I\'m creating an app with the Google Maps API where I want

相关标签:
3条回答
  • 2021-01-27 06:35

    Try:

    PHP:

    function getMarkers(){
        $response = array();
    
        foreach ($query as $post) {
    
        $response[] = array(
            'name' => $post['naam'],
            'lat' => $post['plaats'],
            'lng' => $post['categorie'],
        );
    }
    echo json_encode($response);
    }
    

    then JS:

    function addMarkers(json){
        var title =json[index]["naam"];
        var lat =json[index]["plaats"];
        var lng =json[index]["categorie"];
    
        marker = new google.maps.Marker({
            position: new google.maps.LatLng (lat, lng),
            map: YOUR_MAP
        });
    }
    
    jQuery.getJSON("URL OF PAGE",{
        format: "json",
        dataType: 'json',
        contentType: "application/json; charset=utf-8"},
        function(json){addMarkers(json);
    });
    

    better, to send the info with json and retrieve it with jQuery's getJSON, this may need some tweaking.. But I have created what your talking about. Shout if any further help is required.

    You May have to look into this a tiny bit to configure it..

    GOOD LUCK!

    0 讨论(0)
  • 2021-01-27 06:36

    $post is your iteration variable, so it only contains one element of the array at a time. If you want the entire array, assign that:

    var bedrijven = <?php echo json_encode($query); ?>;
    
    0 讨论(0)
  • 2021-01-27 06:52

    The examples on http://us1.php.net/json_encode should be helpful. It looks like your $post should be an associative array:

    $post[] = ("name"=>"test","lat"=>52.351753, "lon"=> 5.002035); ?>
    var bedrijven = <?php echo json_encode($post); ?>;
    

    outputs:

    var bedrijven = [{"name":"test","lat":52.351753,"lon":5.002035}];

    0 讨论(0)
提交回复
热议问题