开发者

Query with codeigniter form helper

开发者 https://www.devze.com 2023-02-27 09:23 出处:网络
I\'m working on a radius search with Google Maps API and MYSQL (http://spinczyk.net/blog/2009/10/04/radius-search-with-google-maps-and-mysql/)

I'm working on a radius search with Google Maps API and MYSQL (http://spinczyk.net/blog/2009/10/04/radius-search-with-google-maps-and-mysql/)

Is is possible to use CI form helper functions for the following query?

SELECT
`id`,
`name`,
ACOS( SIN( RADIANS( `latitude` ) ) * SIN( RADIANS( $fLat ) ) + COS( RADIANS( `latitude` ) )
* COS( RADIANS( $fLat )) * COS( RADIANS( `longitude` ) - RADIANS( $fLon )) ) * 6380 AS `distance`
FROM `stations`
WHERE
ACOS( SIN( RADIANS( `latitude` ) ) * SIN( RADIANS( $fLat ) ) + COS( RADIANS( `latitude` ) )
* COS( RADIANS( $fLat )) * COS( RADIANS( `longitude` ) - RADIANS( $fLon )) ) *开发者_如何学Go 6380 < 10
ORDER BY `distance`

I'm not sure how to add all that using form helper.


If you want to use database in your helpers, you need access to the CI super object. In your function:

$ci =& get_instance();
$ci->load->database();
$sql = "YOUR SQL QUERY GOES HERE";
$q = $ci->db->query($sql);
if($q->num_rows() > 0)
{
   //Process your query here...
}


There is no way to create that query with the database class as is.

You can use:
$this->db->query()
http://codeigniter.com/user_guide/database/queries.html

Or you could extend the database class yourself to include math functions.


For re-usability reasons, i prefer to create a helper for that :

https://gist.github.com/lefakir/e84c22996b1e77436f955c5a7df4d671

And then use it in other helpers :

$ci = get_database_connected_ci();
$query = $ci->db->query($sql);
0

精彩评论

暂无评论...
验证码 换一张
取 消