使用php从JSON中提取数据并在HTML下拉菜单中实现

ymdaylpp  于 2022-10-22  发布在  PHP
关注(0)|答案(1)|浏览(124)

我已经设置了一个任务,创建一个PHP例程,从网站的of countries下拉列表中的geojson返回ISO代码和名称。
这是一个全新的概念,并且在文档中非常困难。
我在导航栏中创建了一个html下拉列表

<nav id="navbar" class="navbar">
        <ul>
          <li class="dropdown"><a href="#"><span>Please Select a Country to Learn More!</span> <i class="bi bi-chevron-down"></i></a>

        <i class="bi bi-list mobile-nav-toggle"></i>
      </nav>

这是我提供的GeoJSON文件的示例

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "name": "Bahamas",
                "iso_a2": "BS",
                "iso_a3": "BHS",
                "iso_n3": "044"
            },
            "geometry": {
                "type": "MultiPolygon",
                "coordinates": [
                    [
                        [
                            [
                                -77.53466,
                                23.75975
                            ],
                            [
                                -77.78,
                                23.71
                            ],
                            [
                                -78.03405,
                                24.28615
                            ],
                            [
                                -78.40848,
                                24.57564
                            ],
                            [
                                -78.19087,
                                25.2103
                            ],
                            [
                                -77.89,
                                25.17
                            ],
                            [
                                -77.54,
                                24.34
                            ],
                            [
                                -77.53466,
                                23.75975
                            ]
                        ]
                    ],

我想做的是确保在我的下拉列表中可以找到geojson中的所有国家/地区名称,并且当选中时,它们会导航到传单Map上的正确区域。
我现在已经设置好了传单Map。
我尝试过PHP脚本,但不知道我是否在正确的轨道上,也不知道如何使用它来实现我想要的

<?php 

$executionStartTime = microtime(true) / 1000;

$json = json_decode(file_get_contents("../geojson/countries_small.geo.json"), true);

    foreach ($json['features'] as $feature) {
            $temp = null;
            $temp['code'] = $feature["id"];
            $temp['name'] = $feature['properties']['name'];

            array_push($country, $temp);
        }

    $output['status']['code'] = "200";
    $output['status']['name'] = "ok";
    $output['status']['description'] = "mission saved";
    $output['status']['returnedIn'] = (microtime(true) - $executionStartTime) / 1000 . " ms";
    $output['data'] = $decode;

    header('Content-Type: application/json; charset=UTF-8');

    echo json_encode($output); 

?>
6tdlim6h

6tdlim6h1#

您有几个未初始化的变量(可读性是关键^_^)如果我正确理解了您要做的事情,那么我将如何做(我刚刚调整了您现有的代码)

<?php 

$executionStartTime = microtime(true) / 1000;

try {
$json = json_decode(file_get_contents("../geojson/countries_small.geo.json"), true);
$country = array();

foreach ($json['features'] as $feature) {
    $country[$feature["id"]] = $feature['properties']['name'];
}

//Rather use proper header codes, unless you really need to send this
/*
$output['status']['code'] = "200";
$output['status']['name'] = "ok";

* /

$output['status']['description'] = "mission saved";
$output['status']['returnedIn'] = (microtime(true) - $executionStartTime) / 1000 . " ms";
$output['data'] = $country;

http_response_code(200);
header('Content-Type: application/json; charset=UTF-8');

echo json_encode($output);
} catch (Exception $Exception) {
    http_response_code(500);

    //display $Exception->getMessage(), or whatever you need to do here if there's an error
}
?>

相关问题