Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query with codeigniter form helper

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.

like image 776
vir Avatar asked Feb 12 '26 06:02

vir


2 Answers

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...
}
like image 162
zokibtmkd Avatar answered Feb 14 '26 20:02

zokibtmkd


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.

like image 43
afuzzyllama Avatar answered Feb 14 '26 21:02

afuzzyllama



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!