如何动态设置位置

时间:2013-05-09 04:45:00

标签: php json passbook

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

$pass->setJSON('{...........
"locations" : [
    {
      "longitude" : 104.89529371261597,
      "latitude" : 11.576150037278605,
      "relevantText": "CamMob (dis. 1%)"
    },
    {
      "longitude" : 104.89823341369629,
      "latitude" : 11.570705493829758,
      "relevantText": "ITC (dis. 2%)"
    },
    {
      "longitude" : 104.91380631923676,
      "latitude" : 11.553425198985883,
      "relevantText": "Digi (dis. 10%)"
    },
    {
      "longitude" : 104.92096781730652,
      "latitude" : 11.569580835552655,
      "relevantText": "Central market (dis. 10%)"
    },
    {
      "longitude" : 104.89310503005981,
      "latitude" : 11.569444194519487,
      "relevantText": "IFL (dis. 20%)"
    },
    {
      "longitude" : 104.89041209220886,
      "latitude" : 11.568655879564044,
      "relevantText": "RUPP (dis. 5%)"
    },
    {
      "longitude" : 104.89643096923828,
      "latitude" : 11.568613836037402,
      "relevantText": "Children hospital (dis. 50%)"
    },
    {
      "longitude" : 104.89986419677734,
      "latitude" : 11.566343476220418,
      "relevantText": "Santhormuk high school (dis. 15%)"
    },
    {
      "longitude" : 104.89693522453308,
      "latitude" : 11.573774631737194,
      "relevantText": "Royal ratanak hospital (dis. 25%)"
    },
    {
      "longitude" : 104.89693522453308,
      "latitude" : 11.573774631737194,
      "relevantText": "AEU (dis. 30%)"
    },
    {
      "longitude" : 104.8942369222641,
      "latitude" : 11.561511017118434,
      "relevantText": "Moon Car Wash (dis. 10%)"
    },
    {
      "longitude" : 104.92413818836212,
      "latitude" : 11.553772076702026,
      "relevantText": "Auto Spa (dis. 10%)"
    },
    {
      "longitude" : 104.9108076095581,
      "latitude" : 11.545604569281316,
      "relevantText": "Penh Chet (dis. 5%)"
    },
    {
      "longitude" : 104.89751189947128,
      "latitude" : 11.590985354685541,
      "relevantText": "E-garage (dis. 5%)"
    },
    {
      "longitude" : 104.91095378994942,
      "latitude" : 11.562604174491218,
      "relevantText": "Futaba Garage (dis. 5%)"
    },
    {
      "longitude" : 104.91638526320457,
      "latitude" : 11.55557478276799,
      "relevantText": "Haojue Motorcycle (dis. 3%)"
    },
    {
      "longitude" : 104.90114092826843,
      "latitude" : 11.545636104516502,
      "relevantText": "Ice City Skating Rink (dis. 20%)"
    },
    {
      "longitude" : 104.93705168366432,
      "latitude" : 11.5573354320172,
      "relevantText": "Galaxkate (dis. 10%)"
    },
    {
      "longitude" : 104.92472290992737,
      "latitude" : 11.553572359281521,
      "relevantText": "G-rise photography (dis. 20%)"
    },
    {
      "longitude" : 104.92827415466309,
      "latitude" : 11.556431458262171,
      "relevantText": "Portor School International Kindergarten (dis. 5%)"
    },
    {
      "longitude" : 104.92472290992737,
      "latitude" : 11.553572359281521,
      "relevantText": "Cambridge International School of Cambodia (dis. 15%)"
    },
    {
      "longitude" : 104.89523202180862,
      "latitude" : 11.569042154169637,
      "relevantText": "EZECOM (dis. 35.5%)"
    }
  ]
    }');

3 个答案:

答案 0 :(得分: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将返回numericaly索引的php数组作为json数组和带有字符串索引的数组作为javascript对象...希望这有帮助

答案 1 :(得分: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);

?>

答案 2 :(得分: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);