0

I have many locations to put in pass. Before, I just set those locations statically. Now, I create a company table and I create an interface for adding, deleting and updating the detail of company already. Thus, I should create those location of company since I create a pass. Therefore, how should I set them dynamically ? I use code like this for static locations:

$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%)"
    }
  ]
    }');

11
  • Fetch the records, produce an array as wanted & encode it into a json string.
    – Rikesh
    May 9, 2013 at 4:47
  • How do I use array to set them all by selecting from company table ?
    – malinchhan
    May 9, 2013 at 4:47
  • "locations" : [ { "longitude" : "'.$long.'", "latitude" : "'.$lat.'", "relevantText": "'.$name.' '.$discount.'" }}
    – malinchhan
    May 9, 2013 at 4:57
  • so if 1 location has 4 elements , so I have to have 4 arrays ?
    – malinchhan
    May 9, 2013 at 5:01
  • As I said you need to re-format the array as you want. Can you post your resultant array in question ?
    – Rikesh
    May 9, 2013 at 5:03

3 Answers 3

2

do

$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 will return numericaly indexed php arrays as json arrays and arrays with string indexes as javascript objects... hope this helps

4
  • Also remember that a pass can only have 10 locations. The rest would be ignored so you should add LIMIT 10 to the query, and should probably also add a WHERE or ORDER BY condition to provide some logic of which 10 will be selected.
    – PassKit
    May 9, 2013 at 5:12
  • 1
    looking at the json you are trying to reproduct the last line should actually read return json_encode(array("locations"=>$ret));
    – Orangepill
    May 9, 2013 at 5:13
  • Why I can only use 10 locations ?
    – malinchhan
    May 9, 2013 at 7:22
  • gHow many time have I sent you a link to the documentation!!! Read it, it will tell you there is a limit. You even posted a question asking why you were seeing an error because of too many locations! Apple have decided that the limit is 10. Just imagine how big a pass would be if it had every single Starbucks or McDonnalds location in it. These files have to be downloaded over cellular networks - if they are too large, the user experience will suffer.
    – PassKit
    May 9, 2013 at 15:54
1

Try like this..

<?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);

?>
0
1

I just try this and I can see the result like what I wrote statically:

$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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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