On my site (aimed at PS3 gamers), users can select the 3 games they're currently playing. I will then work out which games are the most popular, and list the top 5 based on active users.
I have 3 columns in my users table called game1_id, game2_id and game3_id. These values are relational to the id column in another table called game, which contains all of the information on the games such as it's title.
How would I go about tallying up these totals and returning the top 5 most active games?
Here is my model function thus far:
function get_5_popular_games()
{
$this->db->select('user.game1_id, user.game2_id, user.game3_id');
$this->db->from('user');
$query = $this->db->get();
if ($query->num_rows() > 0)
{
return $query->result();
}
}
I assume I need to somehow tally up how many times a games id is found in any of the three columns (game1_id, game2_id, game3_id), but I'm not entirely sure how to go about it?
[EDIT] AND HERE IS THE FINISHED FUNCTION.
function get_popular_games($limit)
{
$this->db->select('count(*) as popularity, game.title');
$this->db->from('user_game');
$this->db->join('game', 'game.id = user_game.game_id');
$this->db->group_by('game_id');
$this->db->order_by('popularity', 'desc');
$this->db->limit($limit);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
return $query->result();
}
}