Welcome Guest, Not a member yet? Register   Sign In
Need help converting SQL statement to Active Records
#1

[eluser]Unknown[/eluser]
I don't know why I never used Codeigniter to develop websites, but I just started using it 2 weeks ago, and it is awesome!

I've been a PHP developer for about 4 years now, and I know my way with basic SQL statements, but ever since reading this post http://ellislab.com/forums/viewthread/116566/ I now know that I've been doing SQL commands improperly, basically if I wanted to get information from 2 tables, I would query the primary table, then in a WHILE loop, I would query the Secondary table.

I have taken Pixel888's SQL statement and modified it for a zip code radius that works well, everything is working great, but I want to do more Active Record SQL statements, and I have a problem trying to convert this (complicated to me) statement. I tried all I could to make it work myself, but I cannot get past it. Here is the SQL code that is working great inside my MODEL file.

Code:
function getLatLong($zipcode) {
  $this -> db -> select('latitude, longitude');
  $this -> db -> from('zipcodes');
  $this -> db -> where('zipcode', $zipcode);
  $q = $this -> db -> get();
  foreach ($q->result() as $row) {
   return $row;
  }
}

function zipcodeRadius($lat, $lon, $radius, $offset = 0) {
  $radius = $radius ? $radius : 20;
  $sql = 'SELECT u.*, z.latitude, z.longitude,
  (3956 * 2 * ATAN2(SQRT(POW(SIN((RADIANS(latitude) - RADIANS(' . $lat . '))/2),2) + COS(RADIANS(' . $lat . ')) * COS(RADIANS(latitude)) *
  POW(SIN((RADIANS(longitude) - RADIANS(' . $lon . '))/2),2)),SQRT(1-(POW(SIN((RADIANS(latitude) - RADIANS(' . $lat . '))/2),2) +
  COS(RADIANS(' . $lat . ')) * COS(RADIANS(latitude)) * POW(SIN((RADIANS(longitude) - RADIANS(' . $lon . '))/2),2))))) as DISTANCE
  FROM users u, zipcodes z WHERE u.zipcode = z.zipcode HAVING DISTANCE <= 20 order by distance asc LIMIT '. $offset . ', 5';  
  $q['query'] = $this -> db -> query($sql);
  $q['num_rows'] = $this -> db -> query($sql) -> num_rows();

  return $q;
}


I want to be able to take that SQL code, and convert it into active record SQL, but I don't know where to start. I tried doing

Code:
$this->db->select('u.*, z.latitude, z.longitude, (3956 * 2 * ATAN2(SQRT(POW(SIN((RADIANS(latitude) - RADIANS(' . $lat . '))/2),2) + COS(RADIANS(' . $lat . ')) * COS(RADIANS(latitude)) *
  POW(SIN((RADIANS(longitude) - RADIANS(' . $lon . '))/2),2)),SQRT(1-(POW(SIN((RADIANS(latitude) - RADIANS(' . $lat . '))/2),2) +
  COS(RADIANS(' . $lat . ')) * COS(RADIANS(latitude)) * POW(SIN((RADIANS(longitude) - RADIANS(' . $lon . '))/2),2))))) as DISTANCE');
$this->db->from('users u, zipcodes z');
$this->db->where('u.zipcode', 'z.zipcode');

etc, etc... I would have written the rest, but the problem is, when I did the whole active record statement, and error came up on the view, and the SQL statement has single quotes around the beginning c.*, z.latitude, z.longitude, so I know it's not working.

Any help would be greatly appreciated in the conversion. I actually don't know if this would be a join or not, since I have never worked with joins before, but I don't see a JOIN statement in the original SQL itself, unless it's shorthand JOIN.

Thanks in advance!




Theme © iAndrew 2016 - Forum software by © MyBB