0

Fetching data from mySQL and Generating json through php like this:

while ($row = mysql_fetch_array($result)) {
     $menu[] = array("type"=>"FeatureCollection", 
                    "features" => [array("type"=>"Feature",
                                         "geometry"=>array("type"=>"Point",
                                                           "coordinates"=>[-77.034084142948,38.909671288923]),
                                         "properties"=>array("phone"=>"041","city"=>"Faisalabad","country"=>"Pakistan"))]       
        );
}   $json =  json_encode($menu); 
    echo $json;

and the result looks like this:

[
{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -77.034084142948,
                    38.909671288923
                ]
            },
            "properties": {
                "phone": "041",
                "city": "Faisalabad",
                "country": "Pakistan"
            }
        }
    ]
},
{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -77.034084142948,
                    38.909671288923
                ]
            },
            "properties": {
                "phone": "041",
                "city": "Faisalabad",
                "country": "Pakistan"
            }
        }
    ]
},
{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -77.034084142948,
                    38.909671288923
                ]
            },
            "properties": {
                "phone": "041",
                "city": "Faisalabad",
                "country": "Pakistan"
            }
        }
    ]
}]

As you can see {"type": "FeatureCollection","features":[{...}]}is being repeated three time, I want it to appear at the beginning only like following json, so that {"type": "FeatureCollection","features":[{I WANT LOOP HERE only}]}: I want this result:

[
{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -77.034084142948,
                    38.909671288923
                ]
            },
            "properties": {
                "phone": "041",
                "city": "Faisalabad",
                "country": "Pakistan"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -77.034084142948,
                    38.909671288923
                ]
            },
            "properties": {
                "phone": "041",
                "city": "Faisalabad",
                "country": "Pakistan"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -77.034084142948,
                    38.909671288923
                ]
            },
            "properties": {
                "phone": "041",
                "city": "Faisalabad",
                "country": "Pakistan"
            }
        }
    ]
}]

Please help I have been trying too much. Thanks

1
  • If you don't want the array the way you're currently doing, then change the array creation in your while-loop. Commented Dec 7, 2014 at 12:01

2 Answers 2

1

Well, why do you keep repeating "FeatureCollection" in the while-loop if you only want it once?

I think you want something like this:

$menu = array();
features = array();
$menu['type'] = 'FeatureCollection';

while ($row = mysql_fetch_array($result)) {
     $features[] = array("type"=>"Feature",
                       //Other features here
      );
}

$menu['features'] = $features;
Sign up to request clarification or add additional context in comments.

Comments

1

In your $menu array you shoud define key type with value FeatureCollection, and key features. And in your while loop do this:

$menu = array(
    'type' => 'FeatureCollection',
    'features' => array()
);
while ($row = mysql_fetch_array($result)) {
    $menu[`features`][] = array(...);
}

1 Comment

what about square brackets wraping it.. missing how do i add it

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.