我正在开发一个使用php进行汽车销售的网站。我正在使用代码点火器框架。 有一个搜索页面,可以选择查找最近的车辆。 我使用以下查询来获取汽车列表
$given_distance=100;
$q = "SELECT SQL_CALC_FOUND_ROWS GROUP_CONCAT(img_name) AS images, cars.id,cars.user,cars.status,cars.engine_size,cars.price_max,cars.title,cars.distance as distance_covered,date_format(cars.date_posted,'%d-%M') as date_posted,body.body,fuel.fuel,make.make as make,company.company,model.model,users.firstname,users.role,users.phone,users.lastname,users.latitude as map_latitude,users.longitude as map_longitude,users.city as map_city,user_payment.status as payment_status,color.color as color,color.code as color_code,transmission.transmission ";
if($longitude&&$latitude&&$distance&&$postcode!=''){$q.=" , CAST(3959 * 2 * ASIN(SQRT(POWER(SIN(({$latitude} - `latitude`) * pi()/180 / 2), 2) + COS({$latitude} * pi()/180) * COS(`latitude` * pi()/180) * POWER(SIN(({$longitude} - `longitude`) * pi()/180 / 2), 2) )) as DECIMAL(10, 2))*1.5 AS distance ";}else{$q.=',0 as distance '; }
$q.=" from cars left join users on cars.user=users.id left join make on cars.make=make.id where cars.status=1 having distance<=$given_distance";
查询工作正常,我得到结果。但问题是查询计算直线距离,但我需要驾驶距离,如谷歌地图
我找到了一个用于查找驾驶距离的代码,如下所示
/*
* function for calculating distance between two places
* @parm $from,$to
* @return
*/
public function find_distance($from=688538,$to=682018)
{
$start = $from;
$destination = $to;
$mode = "driving";
$xml = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/xml?origins=$start&destinations=$destination&mode=$mode&language=en-EN&sensor=false");
$data = new SimpleXMLElement($xml);
$distance = $data->row->element->distance->text;
$time = $data->row->element->duration->text;
if(empty($distance) OR empty($time)){
return $to;
//echo 'reverr';die();
}else{
return floatval($distance);die();
}
}//find distance
现在问题是我如何在mysql查询中使用这个函数
答案 0 :(得分:0)
$sql = "SELECT `place1`, `place2` FROM table WHERE id = 'something';
$query = $ci->db->query($sql);
$arr = array();
foreach ($query->result() as $row){
$arr[] = find_distance($from=$row['place1'],$to=$row['place2']);
}
print_r($arr);
以上内容应该为您提供所需的内容(当您插入正确的字段名称时!)