CodeIgniter Forums
quick question on 'math' in codeigniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: quick question on 'math' in codeigniter (/thread-9403.html)



quick question on 'math' in codeigniter - El Forum - 06-24-2008

[eluser]Unknown[/eluser]
I have a quick question about CodeIgniter. A couple years back, in PHP/ADOdb411, I created a system that allowed fans of a soccer team to come the site and rate the players on their performance 1-10, then display the average rating.

The way I had it structured was I had the following tables in MySQL: player, game, rating. The rating table had the rating value, but also had foreign keys for the player and the game. Then whenever the games page was displayed I'd set my query to AVG(rating_value) as average_rating for each player.

Is this possible to do in CodeIgniter? Is there a better way than I did it previously?


quick question on 'math' in codeigniter - El Forum - 06-24-2008

[eluser]Pachirulo[/eluser]
Everything is possible with CI but, i think u are using SQL not PHP to do an average.

I think the better way is doing AVG at SQL, I think is always faster.


quick question on 'math' in codeigniter - El Forum - 06-24-2008

[eluser]Unknown[/eluser]
[quote author="Pachirulo" date="1214332253"]Everything is possible with CI but, i think u are using SQL not PHP to do an average.

I think the better way is doing AVG at SQL, I think is always faster.[/quote]

you're right, i meant that i was doing AVG in my SQL query, not in the PHP.

i'll have to do some digging now to find out how to use the AVG in the SQL query with CI. any tips on what section(s) to pay close attention to?


quick question on 'math' in codeigniter - El Forum - 06-24-2008

[eluser]Pachirulo[/eluser]
yes, at user guide... or you can use something like
Code:
$this->db->query('select * from mytable... etc etc');

take a look here: http://ellislab.com/codeigniter/user-guide/database/index.html


quick question on 'math' in codeigniter - El Forum - 06-24-2008

[eluser]xwero[/eluser]
$this->db->select_avg();

See Active Record page