使用ajax调用php函数,在该函数内部调用不会触发的另一个函数

时间:2018-09-25 17:17:41

标签: javascript php ajax wordpress woocommerce

我在前端使用ajax提交wp_query过滤器($ args) 我的functions.php中有一个函数称为filter_function(),这是ajax提交的内容。

在filter_function()内部,我有另一个函数distance_haversine()在我的functions.php文件中被称为thats,但是该函数在filter_function()内部不起作用

示例:

function distance_haversine($lat1, $lon1, $lat2, $lon2) {
      global $earth_radius;
      global $delta_lat;
      global $delta_lon;
      $alpha    = $delta_lat/2;
      $beta     = $delta_lon/2;
      $a   = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($lat1)) *           cos(deg2rad($lat2)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ;
      $c        = asin(min(1, sqrt($a)));
      $distance = 2*$earth_radius * $c;
      $distance = round($distance, 4);

      return $distance;

}


function filter_function(){

if ( isset( $_POST['distance_miles']  ) ) { 

  $distance_miles  = $_POST['distance_miles'];

} else {

   $distance_miles  = 50;

}



$args = array(
    'orderby' => 'date', 
    'order' => $_POST['date'], 
    'post_type' => 'product', 
    'posts_per_page' => -1,
    's' => $_POST['search'],
);

$query = new WP_Query( $args );

if( $query->have_posts() ) :
    while( $query->have_posts() ): $query->the_post();
      $event_lat     = get_post_meta(get_the_ID(),'prod-lat',true);
      $event_long    = get_post_meta(get_the_ID(),'prod-lng',true);
      $full_location = get_post_meta(get_the_ID(),'prod-full-address',true); 
      $earth_radius = 3960.00; # in miles
      $lat_1 = $event_lat;
      $lon_1 = $event_long;
      $lat_2 = $user_lat;
      $lon_2 = $user_long;
      $delta_lat = $lat_2 - $lat_1;
      $delta_lon = $lon_2 - $lon_1;

    //$lat_1, $lon_1, $lat_2, $lon_2 all have values

    $hav_distance = distance_haversine($lat_1, $lon_1, $lat_2, $lon_2);//this is not working

    var_dump($hav_distance); //<------**This is outputing "float(0)"**

    if ( $hav_distance <= $distance_miles ) { 

?>

<li>
    <a href="">              
        <?php if ( has_post_thumbnail() ) { ?>
            <?php the_post_thumbnail('thumbnail'); ?>        
        <?php } else { ?>
            <img src="/place-holder.jpeg">
        <?php  }; ?>                
    </a>
</li>

<?php }

  endwhile;

    wp_reset_postdata();

  else :

    echo 'No posts found';

  endif;

  die();
}

1 个答案:

答案 0 :(得分:0)

尝试

while not q.empty():             # while the queue is nonempty
    f, current_node, path = q.get()
    if current_node not in visited: # check to avoid duplicate expansions
       visited.add(current_node)    # mark node visited on expansion,
                                    # only now we know we are on the cheapest path to
                                    # the current node.
       if current_node.is_goal:     # if the current node is a goal
          return path               # return its path
       ...