Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysql查询中的Google地图道路距离计算_Php_Sql_Codeigniter_Google Maps - Fatal编程技术网

Php mysql查询中的Google地图道路距离计算

Php mysql查询中的Google地图道路距离计算,php,sql,codeigniter,google-maps,Php,Sql,Codeigniter,Google Maps,我正在使用php开发一个汽车销售网站。我正在使用CodeIgniter框架。 有一个serach页面,可以选择查找最近的汽车。 我使用以下查询获取汽车列表 $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.

我正在使用php开发一个汽车销售网站。我正在使用CodeIgniter框架。 有一个serach页面,可以选择查找最近的汽车。 我使用以下查询获取汽车列表

  $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";
现在的问题是我如何在mysql查询中使用这个函数

$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);

以上内容应能满足您的需要(当您插入正确的字段名时!)

您希望如何在查询中使用该函数?你必须返回lats/longs,然后用每个开始点和结束点调用google api。查询需要函数的结果作为查询内的距离,函数需要查询结果中的一个值,如邮编,但为什么你仍然使用该函数?只需查询lats/longs并使用google API获取距离-如果您想要驾驶距离,您的代码现在为空!您不能在查询中使用来自其他网站的结果-如果您迫切需要这样做,您必须查询一次lat&long,然后再次输入lat,long和distance是从google返回的。谢谢你的评论。这没问题。但问题是在查询结束时,我使用的having需要计算的距离having distance是-这是在CI中完成的,而不是在你的查询中->数组会提供你需要的所有信息。为什么不查询直线距离,比如说8英里(对于10英里的距离),然后使用谷歌API过滤开车时超过10英里的距离——这将是最简单的解决方案,无需对API进行太多调用。
$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);