为什么我的foreach需要永远加载

时间:2018-06-14 21:40:18

标签: javascript php jquery arrays

当我点击我网站上的提交按钮以按地址过滤掉结果时,会运行这段代码。但由于某种原因,页面重新加载需要永远, 如果有人有任何想法我可以如何优化我的代码,请告诉我。

我在下面添加了两个功能,可以解释他们做得更好。

提前致谢。

  <div class="ui-widget">
     <input id="vote" type="text" name="vote"/>
  </div>

所有代码:

  $location = !empty($_POST['saddress']) ? $_POST['saddress'] : "NA";

if ($location != "NA")
    $display = true;
$street = explode(',', $location);
$users = get_users('orderby=nicename&role=employer');
global $wpdb;

$location = $_POST['saddress'];

$street = explode(',', $location);
$cat = $_GET['specializingin'];
//$specilty = 'specialties_'. $cat;


$args = array(
    'meta_query' => array(
        array(
            'key' => 'scategory',
            'value' => $_GET['specializingin'],
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'specialties',
            'value' => ($_GET['specialitiesin'] == 'All Specialties' ? '' : $_GET['specialitiesin']),
            'compare' => 'LIKE'
        ),
    ),
);
//p($args);
$search_users = get_users($args);
//p($search_users);
$formattedAddress = prettyAddress($location);

foreach ($search_users as $k => $user):
    if (!empty($street[0])) {
        $db = prettyAddress(get_user_meta($user->ID, 'company_address', true));
        $match = matchAddress($formattedAddress, $db, count($street));
        if ($match == false)
            unset($search_users[$k]);
    }
endforeach;


$search_users = custom_sorting($search_users);

<form name="jobfilter" action="" method="post">
<div class="filter-srch">

    <input id="locationjf" name="saddress" placeholder="Type desired location" class="search-text1" value="<?= $location != 'NA' ? $location : '' ?>" type="text">

</div>


<div class="filter-srch">

    <input class="job-filter-btn" value="" type="submit">

</div>

}

function prettyAddress($address) {
$location = array();
if (empty($address))
    return $location;

$address = str_replace(" ", "+", $address);
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=$address&key=AIzaSyCR9coRcI1OXCDowXtMYsSNPYAizM8wZD0";
$result = json_decode(file_get_contents($url), true);
if (!isset($result['results'][0]['address_components']))
    return $location;
$components = $result['results'][0]['address_components'];
foreach ($result['results'][0]['address_components'] as $component) {
    switch ($component['types']) {
        case in_array('street_number', $component['types']):
            $location['street_number'] = $component['long_name'];
            break;
        case in_array('route', $component['types']):
            $location['street'] = $component['long_name'];
            break;
        case in_array('sublocality', $component['types']):
            $location['sublocality'] = $component['long_name'];
            break;
        case in_array('locality', $component['types']):
            $location['city'] = $component['long_name'];
            break;
        case in_array('administrative_area_level_2', $component['types']):
            $location['admin_2'] = $component['long_name'];
            break;
        case in_array('administrative_area_level_1', $component['types']):
            $location['state'] = $component['long_name'];
            $location['state_code'] = $component['short_name'];
            break;
        case in_array('postal_code', $component['types']):
            $location['postal_code'] = $component['long_name'];
            break;
        case in_array('country', $component['types']):
            $location['country'] = $component['long_name'];
            $location['country_code'] = $component['short_name'];
            break;
    }
}
return $location;

1 个答案:

答案 0 :(得分:0)

我最终创建了一个cron作业,以从API中获取数据,将结果存储在多个文本文件中,然后从文本文件中获取数据,这比多次查询API快得多。

查询地址解析API日常Cron作业设置

add_action('geocode_daily_event', 'geocode_daily');

function geocode_activation() {
    if ( !wp_next_scheduled( 'geocode_daily_event' ) ) {
        wp_schedule_event( current_time( 'timestamp' ), 'twenty_two_hours', 'geocode_daily_event');
    }
}
add_action('wp', 'geocode_activation');
function geocode_daily(){
    $args = array(
        'meta_query' => array(
            array(
                'key' => 'company_address'
            ),
        ),
    );

    //query execution
    $search_companies = get_users($args);

    foreach ($search_companies as $user){
        $address =  get_user_meta($user->ID, 'company_address', true);
        $address = str_replace(" ", "+", $address);

        $url = "https://maps.googleapis.com/maps/api/geocode/json?address=$address&key={YOUR GEOCODE API KEY HERE}";

        $cache_path = get_stylesheet_directory().'/geocode-cache/';
        $filename = $cache_path.md5($url);

        $result  = file_get_contents($url);
        file_put_contents($filename, $result );
    }
}

查询缓存文件的功能

function prettyAddress($address) {
    $location = array();
    if (empty($address))
        return $location;

    $address = str_replace(" ", "+", $address);
    $url = "https://maps.googleapis.com/maps/api/geocode/json?address=$address&key=AIzaSyCntthdn3101F8RC8RLzv9QxF_tmCvdqNg";

    $cache_path = get_stylesheet_directory().'/geocode-cache/';
    $filename = $cache_path.md5($url);

    // && ( time() - 84600 < filemtime($filename) )
    if( file_exists($filename) ){
        $result = json_decode(file_get_contents($filename), true);

        if (!isset($result['results'][0]['address_components']))
            return $location;
        $components = $result['results'][0]['address_components'];
        foreach ($result['results'][0]['address_components'] as $component) {
            switch ($component['types']) {
                case in_array('street_number', $component['types']):
                    $location['street_number'] = $component['long_name'];
                    break;
                case in_array('route', $component['types']):
                    $location['street'] = $component['long_name'];
                    break;
                case in_array('sublocality', $component['types']):
                    $location['sublocality'] = $component['long_name'];
                    break;
                case in_array('locality', $component['types']):
                    $location['city'] = $component['long_name'];
                    break;
                case in_array('administrative_area_level_2', $component['types']):
                    $location['admin_2'] = $component['long_name'];
                    break;
                case in_array('administrative_area_level_1', $component['types']):
                    $location['state'] = $component['long_name'];
                    $location['state_code'] = $component['short_name'];
                    break;
                case in_array('postal_code', $component['types']):
                    $location['postal_code'] = $component['long_name'];
                    break;
                case in_array('country', $component['types']):
                    $location['country'] = $component['long_name'];
                    $location['country_code'] = $component['short_name'];
                    break;
            }
        }
    }
    else {
        $result  = file_get_contents($url);
        file_put_contents($filename, $result );
        $result  = json_decode($result , true);

        if (!isset($result['results'][0]['address_components']))
            return $location;
        $components = $result['results'][0]['address_components'];
        foreach ($result['results'][0]['address_components'] as $component) {
            switch ($component['types']) {
                case in_array('street_number', $component['types']):
                    $location['street_number'] = $component['long_name'];
                    break;
                case in_array('route', $component['types']):
                    $location['street'] = $component['long_name'];
                    break;
                case in_array('sublocality', $component['types']):
                    $location['sublocality'] = $component['long_name'];
                    break;
                case in_array('locality', $component['types']):
                    $location['city'] = $component['long_name'];
                    break;
                case in_array('administrative_area_level_2', $component['types']):
                    $location['admin_2'] = $component['long_name'];
                    break;
                case in_array('administrative_area_level_1', $component['types']):
                    $location['state'] = $component['long_name'];
                    $location['state_code'] = $component['short_name'];
                    break;
                case in_array('postal_code', $component['types']):
                    $location['postal_code'] = $component['long_name'];
                    break;
                case in_array('country', $component['types']):
                    $location['country'] = $component['long_name'];
                    $location['country_code'] = $component['short_name'];
                    break;
            }
        }
    }
    return $location;
}