1

我得到以下方法的空白输出

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;

        }


}
4

3 回答 3

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)
于 2013-06-25T10:28:03.187 回答
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']);

最后,您的方法需要$dataelse子句中返回。

于 2013-06-25T10:34:21.400 回答
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;

}
于 2013-06-25T11:51:27.237 回答