【问题标题】:PHP to GEOJSON array not looping rows in MYSQL databasePHP到GEOJSON数组不循环MYSQL数据库中的行
【发布时间】:2021-09-12 22:34:40
【问题描述】:

我的 PHP 文件成功连接到 MSQL 数据库并查询 3 个表中的所有列。

$sql = "SELECT * FROM facilitator, address, certification WHERE certification.ID=10002 LIMIT 0,3";

对于本示例,我将输出限制为 3 行。

但我的数组正在生成多组相同用户的数据。它似乎没有遍历数据库的行。这是 PHP 数组

// Create array
$geojson = array('type' => 'FeatureCollection', 'features' => array());

// Loop each row in database
while($row = mysqli_fetch_assoc($result)) {
    $marker = array(
        'type' => 'Feature',
        'properties' => array(
          'Name' => $row['First_Name'],
        ),
        'geometry' => array(
          'type' => 'Point',
          'coordinates' => array( 
            $row["Longitude"], $row["Latitude"]
          )
        )
      );
    array_push($geojson['features'], $marker);
}

$json_string = json_encode($geojson, JSON_PRETTY_PRINT);
echo "<pre>".$json_string."<pre/>";

以及 GEOJSON 结果(限 3 个用户)

{
"type": "FeatureCollection",
"features": [
    {
        "type": "Feature",
        "properties": {
            "Name": "Luz"
        },
        "geometry": {
            "type": "Point",
            "coordinates": [
                "175.298090",
                "-37.791458"
            ]
        }
    },
    {
        "type": "Feature",
        "properties": {
            "Name": "Luz"
        },
        "geometry": {
            "type": "Point",
            "coordinates": [
                "175.298090",
                "-37.791458"
            ]
        }
    },
    {
        "type": "Feature",
        "properties": {
            "Name": "Luz"
        },
        "geometry": {
            "type": "Point",
            "coordinates": [
                "175.298090",
                "-37.791458"
            ]
        }
    }
]

}

您可以看到“Luz”数据每次都重复。你能帮我解决数组不正确的地方吗?谢谢。

【问题讨论】:

    标签: php arrays geojson


    【解决方案1】:

    好的。因此,该数组工作正常,但前面有一个不正确的 SQL 语句,需要合适的 JOIN。像这样的东西提供了一个有效的组合,

    SELECT First_Name, Last_Name, Name
    FROM facilitator
    INNER JOIN certification USING (Client_ID)
    WHERE facilitator.Last_Name='Smith'
    

    我应该指出,JSON 文件坐标用引号 " 括起来,这会产生一个字符串,而不是地图所需的数字。

    【讨论】:

      猜你喜欢
      • 2018-01-21
      • 1970-01-01
      • 2015-02-02
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-06
      • 2015-04-21
      相关资源
      最近更新 更多