Community Resources

‘orderby’ => ‘name’,
‘order’ => ‘ASC’,
‘hide_empty’ => 0,
‘hierarchical’ => 1,
‘number’ => $cat_num,
‘taxonomy’ => ‘listing_category’,
‘pad_counts’ => 1
);

# Get all listing categories
$listing_categories = get_categories($cat_args);
?>

 

$origin = $_POST[‘zip_code’];
if (strlen($origin) != 5){
// If the origin zipcode value is invalid print this to the user
echo ”

“;
echo ”

“. $origin . ” is an invalid zip code

“;
echo ”

“;
}
else {
$args = array(
‘post_type’ => ‘directory_listing’,
‘post_status’ => ‘publish’,
‘numberposts’=> -1,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘listing_category’,
‘field’ => ‘id’,
‘terms’ => $_POST[‘cat’]
)
)
);

$listings = get_posts($args);
// Add listings to the $all_businesses array;
$all_businesses = array();
foreach($listings as $listing){
$url = $listing->guid;
$post_data = get_post_meta($listing->ID);
$business_name = $post_data[‘_ct_text_556e4e979bcb0’][0];
$street = $post_data[‘_ct_text_556e4efc64b4c’][0];
$city = $post_data[‘_ct_text_556e4f2d2eff9’][0];
$state = $post_data[‘_ct_text_556e4f5776a4b’][0];
$zip = $post_data[‘_ct_text_556e4fb66fe6c’][0];
$address = $street . ” ” . $city . ” ” . $state . ” ” . $zip;
$phone = $post_data[‘_ct_text_556e4f8aa5244’][0];
$email = $post_data[‘_ct_text_556e504676cca’][0];

$hp_listing = array(
‘provider’ => $post_data[‘_ct_text_556e4d3a285b0’][0] . ” ” . $post_data[‘_ct_text_556e4e1c910f9’][0],
‘url’ => $url,
‘business_name’ => $business_name,
‘address’ => $address,
‘phone’ => $phone,
’email’ => $email
);

//Add business to the array of all businesses
array_push($all_businesses, $hp_listing);
}

$all_addresses = array();
foreach($all_businesses as $hp){
array_push($all_addresses, $hp[‘address’]);
}

// Chunks of addresses
// We have more than 40 addresses so we chunk them to prevent Google’s Distance
// Matrix API from timing out. Each chunk will contain a max of 30 departure/destination pairs
$address_chunks = array();
$count_all_businesses = count($all_businesses);
if ($count_all_businesses > 40){
$address_chunks = array_chunk($all_businesses, 30);
}else{
array_push($address_chunks, $all_businesses);
}

$index = 0;
$key_index = 0;
$chunk_addresses = array();
// For each address chunk, get the addresses and make a call to Google
$api_keys_array = array(‘AIzaSyAVPXmHEqhv8YcpuOTBJp0mMDdeRNuM7PA’,
‘AIzaSyDHRei37bCKJeQWbZ8rBEY6zVKt3owHO3s’,
‘AIzaSyCHh1aBwpzyATt9HO0Y1iiJI_usahlJvNk’,
‘AIzaSyBN0H8QtLIbZypJEHugvnJyxuGYGaF3JYQ’,
‘AIzaSyBHsSPfeENGH4iLcJtW-7Ahm9YjOqoekeU’
);
foreach($address_chunks as $chunk){
foreach($chunk as $rec){
array_push($chunk_addresses, $rec[‘address’]);
}
$hp_addresses = join(‘|’, $chunk_addresses);
$chunk_addresses = array(); // Reset the chunk_addresses array
$api_key = $api_keys_array[$key_index];
$destination = $hp_addresses;
$url=’https://maps.googleapis.com/maps/api/distancematrix/json?origins=’. urlencode($origin) . ‘&destinations=’ . urlencode($destination) . ‘&units=imperial&key=’ . $api_key;
$response = file_get_contents($url);
$response = json_decode($response);
$status = $response->status;

// If we are over status limt for that key
// Use a different api key
while($status == ‘OVER_QUERY_LIMIT’){
if ($key_index < count($api_keys_array)){ $key_index += 1; $api_key = $api_keys_array[$key_index]; $url=’https://maps.googleapis.com/maps/api/distancematrix/json?origins=’. urlencode($origin) . ‘&destinations=’ . urlencode($destination) . ‘&units=imperial&key=’ . $api_key; $response = file_get_contents($url); $response = json_decode($response); $status = $response->status;
}
}

$number_of_recs = count($response->rows[0]->elements);

for ($i = 0; $i < $number_of_recs; $i++){ $miles = $response->rows[0]->elements[$i]->distance->text;
$distance_value = $response->rows[0]->elements[$i]->distance->value;

//Store the distance values of each hp found
$hp = $all_businesses[$index];
$hp[‘miles’] = $miles;
$hp[‘distance’] = $distance_value;
$all_businesses[$index] = $hp;
$index = $index + 1;
}
}
//Get Health professionals sorted by distance to you
$hp_distances = array();
foreach ($all_businesses as $key => $row)
{
$hp_distances[$key] = $row[‘distance’];
}
array_multisort($hp_distances, SORT_ASC, $all_businesses);

// Loop through sorted Hps and print value to screen
echo ”

“;
echo ”

    • “;
    • $name = get_cat_name($_POST[‘cat’]);
    • echo “

“. $name . ” Health Provider(s) found: ” .count($listings) . “

“;

foreach ($all_businesses as $hp){
echo ”

    • “;
      echo ”

      ” . $hp[‘miles’] . ” from you

      “;
      echo ”

      Provider: ” . $hp[‘provider’] . “

      “;
      echo ”

      Business: ” . $hp[‘business_name’] . “

      ” ;
      echo ”

      Address: ” . $hp[‘address’] . “

      “;
      if (!empty($hp[‘phone’])){
      echo ”

      Phone Number: ” . $hp[‘phone’] . “

      ” ;
      }
      if (!empty($hp[’email’])){
      echo ”

      Email: ” . $hp[’email’] . “

      “;
      }
      $url = $hp[‘url’];
      echo ”

      Click to view full listing

      “;
      echo “

“;
}
echo ”

“;
echo ”

“;
}
?>