db_onelite= $this->load->database("onelite",true); } function nearest() { $lat = $this->sys_input["lat"]; $lng = $this->sys_input["lng"]; $max = $this->sys_input["max"]?$this->sys_input["max"]:500; $sql = "select M_CityID,M_CityName, distance(?,?,M_BranchLat,M_BranchLng)/1000 distance from m_branch JOIN m_city ON M_BranchM_CityID = M_CityID having distance < ? order by distance "; $qry = $this->db_onelite->query($sql, array($lat,$lng,$max)); echo $this->db_onelite->last_query(); if (! $qry ) { //echo $this->db_onelite->last_query(); echo json_encode( array( "status" => "ERR", "message" => print_r($this->db_onelite->last_query(),true) )); return; } $rows = $qry->result_array(); $sql = "select M_CityID as id, M_CityName as name from m_branch JOIN m_city ON M_BranchM_CityID = M_CityID GROUP BY M_BranchM_CityID"; $qry = $this->db_onelite->query($sql); if (! $qry ) { echo json_encode( array( "status" => "ERR", "message" => print_r($this->db_onelite->last_query(),true) )); return; } $cities = $qry->result_array(); $selected_city = array('id'=>'','name'=>''); if($rows){ $selected_city['id'] = $rows[0]['M_CityID']; $selected_city['name'] = $rows[0]['M_CityName']; }else{ $sql = "select M_CityID as id, M_CityName as name from m_branch JOIN m_city ON M_BranchM_CityID = M_CityID GROUP BY M_BranchM_CityID"; $qry = $this->db_onelite->query($sql); $selected_city = $qry->row_array(); } echo json_encode( array( "status" => "OK", "rows" => array('cities'=>$cities,'selected_city'=>$selected_city) )); } } ?>