$value) {
if (strtoupper($value) == “DR” || strtoupper($value) == “DR.” || strtoupper($value) == “DRS” || strtoupper($value) == “DRS.”) {
unset ($name[$key]);
}
}
if ($name) { //Make sure we didn’t just remove the entire dentistname criteria by removing Dr.
$name[‘full’] = implode(” “, $name);
$where_name = ” AND (“;
foreach ($name as $key => $value) {
$where_name .= ‘full_nm LIKE “%’ . $value . ‘%” OR first_nm LIKE “%’ . $value . ‘%” OR last_nm LIKE “%’ . $value . ‘%” OR ‘;
}
$where_name = substr($where_name,0,strlen($where_name) – 4).”)”;
}
}

//Services
if ($service) {
if (is_array($service)) {
$where_service = ” AND m.mbr_id in (SELECT mbr_id from members_services ms WHERE ms.svc_id = “.implode(“) AND m.mbr_id in (SELECT mbr_id from members_services ms WHERE ms.svc_id = “, $service).”)”;
$svcquery = “SELECT svc_nm FROM services WHERE svc_id IN (“.implode(“,”, $service).”) ORDER BY svc_id”;
$result = runCCSQL($svcquery);
while ($svc = mysql_fetch_array($result)) {
$svcname[] = $svc[‘svc_nm’];
}
$criteriamsg[$criteria] = “Services: “.implode(“, “, $svcname);
}
else {
$from_service = “, members_services ms”;
$where_service = ” AND ms.mbr_id = m.mbr_id”;
$where_service .= ” AND ms.svc_id = ‘”.$service.”‘”;
$criteriamsg[$criteria] = “Service: “.$service;
}
$criteria += 1;
}

if (strlen($country) > 1 && $country != ‘UnitedStates’) {
$criteriamsg[$criteria] = “Country: “.$country;
$criteria += 1;
$where_country = ‘ AND country = “‘.$country.'”‘;
$limit = “”;
$orderby = ‘ ORDER BY m.state, m.city, RAND()’;
}
else {
//Distance
if ($distance) {
$criteriamsg[$criteria] = “Distance: “.$distance.” “.$radius_measure;
$criteria += 1;
if (is_numeric($distance)) {
//Now check the measurement since 1 mile is 1.609344 kilometers or 1 km is 0.621371192 miles
if ($radius_measure == ‘km’) {
$distance = $distance * 0.621371192;
}
}
else {
$errormsg[$error] = “The distance value must be numeric.”;
$error++;
}
}
else {
$distance = 100; //Default radius if not supplied
}

//City State Zip
$citystatezip = false;
if ($city || $state || $zip) {
$citystatezip = true;
if ((($city && !$state ) || (!$city && $state)) && !$zip) {
$criteriamsg[$criteria] = “City: “.ucwords(strtolower($city));
$criteria += 1;
$criteriamsg[$criteria] = “State: “.strtoupper($state);
$criteria += 1;
$errormsg[$error] = “If you want to search using a city or state, you must provide BOTH the city AND the state.”;
$error++;
}
else if (strlen($zip) > 0 && (strlen($zip) != 5 || !is_numeric($zip))) {
$criteriamsg[$criteria] = “Zip Code: “.$zip;
$criteria += 1;
$errormsg[$error] = “The zip code must be five digits.”;
$error++;
}
else {
if ($city && $state && !$zip) {
$criteriamsg[$criteria] = “City: “.ucwords(strtolower($city));
$criteria += 1;
$criteriamsg[$criteria] = “State: “.strtoupper($state);
$criteria += 1;
$zipquery_where = ‘ city = “‘.$city.'” AND state_prefix = “‘.$state.'”‘;
}
else {
//Zip
$criteriamsg[$criteria] = “Zip Code: “.$zip;
$criteria += 1;
$zipquery_where = ” zip_code = “.$zip;
}

$zipquery = “SELECT lat, lon, lon-“.$distance.”/abs(cos(radians(lat))*69) as lonmin, lon+”.$distance.”/abs(cos(radians(lat))*69) as lonmax, lat-(“.$distance.”/69) as latmin, lat+(“.$distance.”/69) as latmax FROM zip_code WHERE”.$zipquery_where;
//echo “zipquery: “.$zipquery.”
“;
$result = runCCSQL($zipquery);
$zip_rows = mysql_num_rows($result);
if ($zip_rows == 0) {
if ($zip) {
$errormsg[$error] = “Unfortunately, “.$zip.” is not a valid zip code.”;
}
else {
$errormsg[$error] = “Unfortunately, “.$city.” “.$state.” is not found. It may exist or may be part of one or more zip code(s) that are associated with another city.”;
}
$error++;
}
else {

while ($zip_all = mysql_fetch_array($result)) {
$lat[] = $zip_all[‘lat’];
$lon[] = $zip_all[‘lon’];
$lonmin[] = $zip_all[‘lonmin’];
$lonmax[] = $zip_all[‘lonmax’];
$latmin[] = $zip_all[‘latmin’];
$latmax[] = $zip_all[‘latmax’];
}
$zipresult[‘lat’] = array_sum($lat)/$zip_rows;
$zipresult[‘lon’] = array_sum($lon)/$zip_rows;
$zipresult[‘lonmin’] = min($lonmin);
$zipresult[‘lonmax’] = max($lonmax);
$zipresult[‘latmin’] = min($latmin);
$zipresult[‘latmax’] = max($latmax);

$select_location = “, 3956 * 2 * ASIN(SQRT( POWER(SIN((“.(float)$zipresult[‘lat’].” – z.lat) * pi()/180 / 2), 2) + COS(“.(float)$zipresult[‘lat’].” * pi()/180) * COS(z.lat * pi()/180) * POWER(SIN((“.(float)$zipresult[‘lon’].” – z.lon) * pi()/180 / 2), 2) )) as distance”;
$from_location = “, zip_code z”;
$where_location = ” AND substring(m.zip,1,5) = z.zip_code AND z.lon between “.$zipresult[‘lonmin’].” and “.$zipresult[‘lonmax’].” and z.lat between “.$zipresult[‘latmin’].” and “.$zipresult[‘latmax’].” having distance < ".$distance; $orderby = ' ORDER BY distance, RAND()'; } } } } } if ($criteriamsg) { echo "

Search Criteria
“;
echo implode(“
“, $criteriamsg).”

“;
echo “

 

“;
}

if ($errormsg) {
echo “

Errors in Search Criteria
“;
echo implode(“
“, $errormsg).”

“;
echo ‘

Please return to the search form to enter valid search criteria or call the Smiles For Life office at (800) 276-9658.

‘;
echo “

 

“;

}
else {
$num = 0;
$select = “SELECT
m.designate,
m.mbr_id,
m.first_nm,
m.last_nm,
m.full_nm,
m.addr1,
m.addr2,
m.city,
m.state,
m.zip,
m.office_ph”;
$from = ” FROM members m”;
$where = ” WHERE no_public_display = ‘no’
AND active_smiles_for_life = ‘yes’
“;

$query = $select . $select_location . ” ” . $from . $from_location . $from_service . ” ” . $where . $where_service . $where_name . $where_country . $where_location . $orderby . $limit;
//echo $query.’
‘;
$result = runCCSQL($query);
if (mysql_num_rows($result) == 0) {
echo ‘

Unfortunately, no Smiles For Life participating dentists currently match your search criteria. Please return to the search form to adjust your search criteria or call the Smiles For Life office at (800) 276-9658.

‘;
}
else {
while ($dentist = mysql_fetch_array($result)){
if ($num == 1) {
$gray = ‘ list_gray’;
$num = 0;
} else {
$gray = ”;
$num = 1;
}
echo ‘

‘;

//Find the right name to display. If the last name doesn’t exist in the full name field, the practice name is in the full name field and we shouldn’t show the designate (i.e. “Dr”)
if (strpos($dentist[‘last_nm’], “&”) === false && strpos(strtoupper($dentist[‘last_nm’]), ” AND “) === false && strpos($dentist[‘full_nm’], trim($dentist[‘last_nm’])) === false)
$displayname = $dentist[‘designate’] . ‘ ‘ . $dentist[‘first_nm’] . ‘ ‘ . $dentist[‘last_nm’];
else
$displayname = $dentist[‘designate’] . ‘ ‘ . $dentist[‘full_nm’];

echo ‘

‘;
if ($dentist[‘addr1’]) {
echo ‘

‘.trim($dentist[‘addr1’]);
if ($dentist[‘addr2’])
echo ‘ ‘.trim($dentist[‘addr2’]);
}
echo ‘, ‘.trim($dentist[‘city’]).’, ‘.trim($dentist[‘state’]).’ ‘.$dentist[‘zip’].’
‘.$dentist[‘office_ph’].’

‘;
if (strlen($country) > 1 && $country != ‘UnitedStates’) {
echo ‘

‘.$country.’

‘;
}
elseif ($citystatezip) {
echo ‘

‘.number_format(round($dentist[‘distance’],1),1).’ miles

‘;
}
echo ‘

‘;
}
echo ‘New Search
Revise SearchPrint‘;
}
}
?>