Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying take data from a MySQL table and create a JSON output in the below format.

{
    markers: [
    {
       latitude: 57.7973333,
       longitude: 12.0502107,
       title: "Angered",
       content: "Representing :)"
    },
    {
       latitude: 57.6969943,
       longitude: 11.9865,
       title: "Gothenburg",
       content: "Swedens second largest city"
    }
  ]
}

Here is the PHP I am using to generate the JSON

$model  = array();

    $query = "SELECT title, content, lat, lng FROM locations_tbl";

    //Get records from database
    $result = mysql_query($query, $con);

    if(mysql_num_rows($result)) {
        while($e = mysql_fetch_assoc($result)) {
            $model['title'][]       = $e['title'];
            $model['content'][] = $e['content'];
            $model['lat'][]     = $e['lat'];
            $model['lng'][]     = $e['lng'];
        }
    }

    header('Content-type: application/json');
    print json_encode(array('marker'=>$model));

Below is the output that I get with the above code:

{
   marker: {
      title: [
          "Marker 1",
          "Marker 2",
          "Marker 3",
          "Marker 4",
          "Marker 5"
       ],
       content: [
          "Text 1",
          "Text 2",
          "Text 3",
          "Text 4",
          "Text 5"
       ],
       lat: [
          "46.99065400",
          "47.03520400",
          "47.20387700",
          "47.62574900",
          "47.43443400"
       ],
       lng: [
          "-122.92164800",
          "-122.81614600",
          "-122.24486400",
          "-122.14453800",
          "-122.46088200"
       ]
   }
}

Any suggestions?

share|improve this question
@user2495292 Your JSON is not valid, you can use this validator to to see validity jsonlint.com – Tahir Yasin Jul 31 at 5:43
I corrected your JSON, check if this is right JSON? pastebin.com/BeLxGZKy – Tahir Yasin Jul 31 at 5:49
add comment (requires an account with 50 reputation)

3 Answers

up vote 1 down vote accepted

Create array like this :

    $i = 0;
    while($e = mysql_fetch_assoc($result)) {
        $model[$i]['title']       = $e['title'];
        $model[$i]['content']     = $e['content'];
        $model[$i]['lat']         = $e['lat'];
        $model[$i]['lng']         = $e['lng'];
        $i++;
    }
share|improve this answer
Thanks that fixed it! – R_Marlatt Jul 31 at 5:47
add comment (requires an account with 50 reputation)

try this

       $i = 0;
       while($e = mysql_fetch_assoc($result)) {
            $model[$i]['title']       = $e['title'];
            $model[$i]['content'] = $e['content'];
            $model[$i]['lat']     = $e['lat'];
            $model[$i]['lng']     = $e['lng'];
            $i++;
        }

I hope it will help

share|improve this answer
add comment (requires an account with 50 reputation)

You never actually create a new array for every marker:

$model = array("markers" => array());
if(mysql_num_rows($result)) {
    while($e = mysql_fetch_assoc($result)) {
        $marker = array();
        $marker['title']    = $e['title'];
        $marker['content']  = $e['content'];
        $marker['lat']      = $e['lat'];
        $marker['lng']      = $e['lng'];
        $model["markers"][] = $marker;
    }
}
share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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