我得到以下方法的空白输出
function getLocation($arr)
{
$data = array();
$qry = $this->db->query('select id,lat,lng from test where lat between'.$arr->lat-0.2 .'and'.$arr->lat+0.2);
$res = $qry->row_array();
if($qry->num_rows()==0)
{
return false;
}
else
{
foreach ($res->result() as $row)
{
$data['distance']=distanceBetween($arr->lat,$arr->lng,$row['lat'],$row['lng']);
$data['id']=$row['id'];
}
return true;
}
}
答案 0 :(得分:2)
当结果来自查询时,您必须返回结果而不是true
。当查询返回任何结果时,您的函数返回true
。
return true;
应该是
return $data;
修改强>
将此查询与()
变量
$qry = $this->db->query('select id,lat,lng from test
where lat between'.($arr->lat)-(0.2) .'and'.($arr->lat)+(0.2));
也
foreach ($res->result() as $row)
应该是
foreach ($qry->result_array() as $row)
答案 1 :(得分:1)
首先你需要:
$res = $qry->result_array(); // not row_array()
其次,你需要:
foreach ($res as $row){
然后,假设distanceBetween
是控制器内的另一种方法,您需要:
$data['distance'] = $this->distanceBetween($arr->lat,$arr->lng,$row['lat'],$row['lng']);
最后,您的方法需要在$data
子句中返回else
。
答案 2 :(得分:0)
正确的代码......现在正在运作
function getLocation($arr)
{
$data = array();
$where='';
$this->db->select('id,lat,lng,address');
$where = 'lat between "'.($arr->lat-0.2).'" AND "'.($arr->lat+0.2).'"';
$this->db->where($where);
$qry = $this->db->get('pa_test');
$res = $qry->result();
$i=0;
if(count($res)>0)
{
foreach($res as $row)
{
$test=$this->distanceBetween($arr->lat,$arr->lng,$row->lat,$row->lng);
$data[$i]['id']=$row->id;
$data[$i]['distance'] = $test;
$i++;
}
}
return $data;
}