2

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 )) ) * 6380 < 10
ORDER BY `distance`

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

1
  • 2
    the form helper is used to make HTML forms. It wont help you here.
    – icchanobot
    Commented Apr 20, 2011 at 1:46

3 Answers 3

7

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...
}
3

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.

1
  • But you can not use $this in the helper functions. You must use: $ci =& get_instance(); $ci->load->database(); $ci->db->query($your_sql_query);
    – mdikici
    Commented Dec 25, 2014 at 8:31
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.