moving sql statements out of views and into controller