使用PHP json_encode()&返回一个JSON对象MySQL传递给jQuery函数 [英] Return a JSON object using PHP json_encode() & MySQL to pass to jQuery function

查看:357
本文介绍了使用PHP json_encode()&返回一个JSON对象MySQL传递给jQuery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从MySQL结果中创建一个json对象,但没有得到我需要的结果。



这是PHP

  $ json = array(); 
$ result = mysqli_query($ connection,$ query);
echo'[';

while($ row = mysqli_fetch_array($ result))
{
echo'{';
echo'latitude:'。$ row ['lat']。',';
echo'longitude:'。$ row ['lng']。',';
echo'icon:'。'./ images /'.$ row ['busColor']。'。png';
echo'}';
}
echo']';

$ jsonstring = json_encode($ json);
echo $ jsonstring;

die();

输出这个

  [{latitude:39.976257,longitude: -  83.003464,icon:./ images / pink.png}] [] 

但我想要这个

  [{latitude:39.976257,longitude: -  83.003464,icon:./ images / pink.png}] 

一旦我得到结果,我需要将该对象传递给一个jQuery插件函数,


$ b

  $。getJSON('myJsonURL,function(myMarkers){
$(#map ).goMap({
markers:myMarkers
});
});

谢谢

解决方案我想正确的做法是:

$ p $ $ json = array();
$ result = mysqli_query($ connection,$ query);
while($ row = mysqli_fetch_array($ result))
{
$ bus = array(
'latitude'=> $ row ['lat'],
'longitude'=> $ row ['lng'],
'icon'=>'./images/'。$ row ['busColor']。'.png'
);
array_push($ json,$ bus);
}

$ jsonstring = json_encode($ json);
echo $ jsonstring;

die();


I'm trying to create a json object from MySQL results, but not getting the result I need.

Here is the PHP

$json = array();
$result = mysqli_query ($connection, $query);
    echo '['; 

        while($row = mysqli_fetch_array ($result))     
        {
            echo '{';
            echo '"latitude":"'.$row['lat'].'",';
            echo '"longitude":"'.$row['lng'].'",';
            echo '"icon":'.'"./images/'.$row['busColor'].'.png"';
            echo '}';    
        }
        echo ']';

        $jsonstring = json_encode($json);
        echo $jsonstring;

        die(); 

It outputs this

[{"latitude":"39.976257","longitude":"-83.003464","icon":"./images/pink.png"}][]

But I want this

[{"latitude":"39.976257","longitude":"-83.003464","icon":"./images/pink.png"}]

once I get the result I need to pass the object to a jQuery plugin function if that makes any difference

$.getJSON('myJsonURL, function(myMarkers){
  $("#map").goMap({
    markers: myMarkers
  });
});

Thanks

解决方案

I guess the correct way to do this would be:

$json = array();
$result = mysqli_query ($connection, $query);
while($row = mysqli_fetch_array ($result))     
{
    $bus = array(
        'latitude' => $row['lat'],
        'longitude' => $row['lng'],
        'icon' => './images/' . $row['busColor'] . '.png'
    );
    array_push($json, $bus);
}

$jsonstring = json_encode($json);
echo $jsonstring;

die();

这篇关于使用PHP json_encode()&返回一个JSON对象MySQL传递给jQuery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆