【问题标题】:How does this PHP filter use ZIP codes?这个 PHP 过滤器如何使用邮政编码?
【发布时间】:2021-06-02 07:01:43
【问题描述】:

此站点 (https://www.helpendhunger.org/find-food-assistance/) 有一个邮政编码搜索输入,可以假设它根据相应的邮政编码返回位置。但我认为情况并非如此。

查看下面的代码,过滤器看起来像是使用关联的纬度和经度值来确定结果。尽管如此,邮政编码肯定会在某个地方发挥作用,但我不知道如何。谁能帮我理解这里发生了什么?

这是我关心的主要部分,后面是完整的代码。

<?php 
    case 'zip':
        $none = !empty($_REQUEST["none"]) ? $_REQUEST['none'] : 'false';
        $lat = $_REQUEST["lat"];
        $lng = $_REQUEST["lng"];
        $x = 0;
        unset($loc_array);
        $loc_array = array();
        foreach ($locations as $loc) {
            $radius = 35;
            $zip_location = $loc['zip_code'];
            $lat_loc = $loc['latitude'];
            $lng_loc = $loc['longitude'];
            $distance = distance($lat, $lng, $lat_loc, $lng_loc, "M");
            if ($distance == $radius) {
                array_push($loc_array, $loc);
            }
        } 
        $count = count($loc_array);
        if ($count > 0 && $none == 'false') {  ?>
            <div class="table">
            <?php foreach ($loc_array as $location) {
                include ('../lib/locations.php');
            } ?>
            <script> var _jsonNew = <?php echo json_encode($loc_array) ?>; </script>
        <?php } else { ?>
            <div class="no_results">
                <div class="in">
                    <div class="response">There were no results for this zip code, please try again.<br><span>or</span></div>
                    <div class="button" data-county="all">View All Locations</div>
                </div>
            </div>
            <script> var _jsonNew = <?php echo json_encode($locations) ?>; </script>
        <?php } ?>

完整代码:

<?php 
require("../wp-load.php");
global $wpdb;
$oper = !empty($_REQUEST["oper"]) ? $_REQUEST["oper"] : die("No Oper");
$sql = "SELECT * FROM locations ORDER BY ID DESC";
$locations = $wpdb->get_results($sql,ARRAY_A);

function sortLocs($a, $b) {
    return ($a["location_title"] > $b["location_title"]) ? 1 : -1;
}

usort($locations, "sortLocs");

if (!function_exists("stripslashes_deep")) {
    function stripslashes_deep($value)
    {
        if (is_array($value)) {
            $value = array_map('stripslashes_deep', $value);
        } else {
            $value = trim($value);
            $value = stripslashes($value);
        }
        return $value;
    }
    
}

$locations = stripslashes_deep($locations);

switch($oper){
    case 'county':
        $county_request = !empty($_REQUEST["county"]) ? $_REQUEST["county"] : die("No County");
        $county = strtolower($county_request);
        $x = 0;
        unset($loc_array);
        $loc_array = array();
        foreach ($locations as $loc) {
            $county_location = strtolower($loc['county']);
            if ($county_location == $county) {
                array_push($loc_array,$loc);
            }
            if ($county=='all') {
                array_push($loc_array,$loc);
            }
        } 
        $count = count($loc_array); ?>
        <div class="table">
        <?php 
        foreach($loc_array as $location) {
            include ('../lib/locations.php');
        }
        ?>
        <script>
            var _jsonNew = <?php echo json_encode($loc_array) ?>;
        </script>
        <?php
    break;
    case 'zip':
        $none = !empty($_REQUEST["none"]) ? $_REQUEST['none'] : 'false';
        $lat = $_REQUEST["lat"];
        $lng = $_REQUEST["lng"];
        $x = 0;
        unset($loc_array);
        $loc_array = array();
        foreach ($locations as $loc) {
            $radius = 35;
            $zip_location = $loc['zip_code'];
            $lat_loc = $loc['latitude'];
            $lng_loc = $loc['longitude'];
            $distance = distance($lat, $lng, $lat_loc, $lng_loc, "M");
            if ($distance == $radius) {
                array_push($loc_array, $loc);
            }
        } 
        $count = count($loc_array);
        if ($count > 0 && $none == 'false') {  ?>
            <div class="table">
            <?php foreach ($loc_array as $location) {
                include ('../lib/locations.php');
            } ?>
            <script> var _jsonNew = <?php echo json_encode($loc_array) ?>; </script>
        <?php } else { ?>
            <div class="no_results">
                <div class="in">
                    <div class="response">There were no results for this zip code, please try again.<br><span>or</span></div>
                    <div class="button" data-county="all">View All Locations</div>
                </div>
            </div>
            <script> var _jsonNew = <?php echo json_encode($locations) ?>; </script>
        <?php } ?>
    
    <?php break;
}
?>

【问题讨论】:

    标签: php html arrays json


    【解决方案1】:

    很难说,但似乎有什么东西正在将邮政编码转换为 lat/lng(很容易在 JS 中使用 Google Maps 完成),并且必须对 lat/ 进行多边形中心搜索lng 他们支持的位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      • 2013-06-15
      • 1970-01-01
      • 2014-08-26
      • 2014-04-13
      • 2013-09-22
      相关资源
      最近更新 更多