0

我有很多地方可以通过。之前,我只是静态设置这些位置。现在,我创建了一个公司表,并创建了一个用于添加、删除和更新公司详细信息的界面。因此,我应该创建公司的那些位置,因为我创建了一个通行证。因此,我应该如何动态设置它们?我将这样的代码用于静态位置:

$pass->setJSON('{...........
“地点”:[
    {
      “经度”:104.89529371261597,
      “纬度”:11.576150037278605,
      "relevantText": "CamMob (dis. 1%)"
    },
    {
      “经度”:104.89823341369629,
      “纬度”:11.570705493829758,
      "relevantText": "ITC (dis. 2%)"
    },
    {
      “经度”:104.91380631923676,
      “纬度”:11.553425198985883,
      "relevantText": "Digi (dis. 10%)"
    },
    {
      “经度”:104.92096781730652,
      “纬度”:11.569580835552655,
      "relevantText": "中央市场 (dis. 10%)"
    },
    {
      “经度”:104.89310503005981,
      “纬度”:11.569444194519487,
      "relevantText": "IFL (dis. 20%)"
    },
    {
      “经度”:104.89041209220886,
      “纬度”:11.568655879564044,
      "relevantText": "RUPP (dis. 5%)"
    },
    {
      “经度”:104.89643096923828,
      “纬度”:11.568613836037402,
      "relevantText": "儿童医院 (dis. 50%)"
    },
    {
      “经度”:104.89986419677734,
      “纬度”:11.566343476220418,
      "relevantText": "Santhormuk high school (dis. 15%)"
    },
    {
      “经度”:104.89693522453308,
      “纬度”:11.573774631737194,
      "relevantText": "Royal ratanak 医院 (dis. 25%)"
    },
    {
      “经度”:104.89693522453308,
      “纬度”:11.573774631737194,
      "relevantText": "AEU (dis. 30%)"
    },
    {
      “经度”:104.8942369222641,
      “纬度”:11.561511017118434,
      "relevantText": "Moon Car Wash (dis. 10%)"
    },
    {
      “经度”:104.92413818836212,
      “纬度”:11.553772076702026,
      "relevantText": "Auto Spa (dis. 10%)"
    },
    {
      “经度”:104.9108076095581,
      “纬度”:11.545604569281316,
      "relevantText": "Penh Chet (dis. 5%)"
    },
    {
      “经度”:104.89751189947128,
      “纬度”:11.590985354685541,
      "relevantText": "E-garage (dis. 5%)"
    },
    {
      “经度”:104.91095378994942,
      “纬度”:11.562604174491218,
      "relevantText": "Futaba Garage (dis. 5%)"
    },
    {
      “经度”:104.91638526320457,
      “纬度”:11.55557478276799,
      "relevantText": "豪爵摩托车 (dis. 3%)"
    },
    {
      “经度”:104.90114092826843,
      “纬度”:11.545636104516502,
      "relevantText": "Ice City Skating Rink (dis. 20%)"
    },
    {
      “经度”:104.93705168366432,
      “纬度”:11.5573354320172,
      "relevantText": "Galaxkate (dis. 10%)"
    },
    {
      “经度”:104.92472290992737,
      “纬度”:11.553572359281521,
      "relevantText": "G-rise Photography (dis. 20%)"
    },
    {
      “经度”:104.92827415466309,
      “纬度”:11.556431458262171,
      "relevantText": "Portor School International Kindergarten (dis. 5%)"
    },
    {
      “经度”:104.92472290992737,
      “纬度”:11.553572359281521,
      "relevantText": "柬埔寨剑桥国际学校 (dis. 15%)"
    },
    {
      “经度”:104.89523202180862,
      “纬度”:11.569042154169637,
      "relevantText": "EZECOM (dis. 35.5%)"
    }
  ]
    }');

4

3 回答 3

2

$qry = mysql_query($conn, "select longitude, latitude, relevantText from location_table");
$ret = array();
while($rec = mysql_fetch_assoc($qry)){
    $ret[] = $rec;
}
// return or print or do something with the data here.
return json_encode(array("locations"=>$ret));

json_encode 将返回数字索引的 php 数组作为 json 数组和带有字符串索引的数组作为 javascript 对象......希望这会有所帮助

于 2013-05-09T05:09:03.860 回答
1

试试这样。。

<?php
$locations = array();
$result = $mysqli->query("select longitude, latitude, relevantText FROM company");

$inc=0;
while ($row = $result->fetch_array()){
        $locations['locations'][$inc]['longitude'] = $row['longitude'];
        $locations['locations'][$inc]['latitude'] = $row['latitude'];
        $locations['locations'][$inc]['relevantText'] = $row['relevantText'];

    $inc++;
    }

echo json_encode($locations);

?>
于 2013-05-09T05:32:28.843 回答
1

我只是试试这个,我可以看到我静态写的结果:

$query5 = mysql_query("select * from company");

while ($row5 = mysql_fetch_array($query5)){ $companyName = $row5['relevantTextName']; $discount = $row5['relevantTextDiscount']; $long = $row5['longitute']; $lat = $row5['latitute']; $link = $row5['link']; $location['locations'][] = array("longitute" => $long, "latitute" => $lat, "relevantText" => $companyName." (" . $discount. "%)" ); } $jsonString = json_encode($location) ; error_log("Locations: ".$jsonString,0);
于 2013-05-10T00:47:40.187 回答