Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7447441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:36:08+00:00 2026-05-29T12:36:08+00:00

On my site (aimed at PS3 gamers), users can select the 3 games they’re

  • 0

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();

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T12:36:10+00:00Added an answer on May 29, 2026 at 12:36 pm

    Instead of having 3 columns, use a separate table to store the favorite games for each user. Then you could use a simple query like the following to get the top 5 games:

    SELECT game_id, count( * ) as popularity
    FROM users_favorite_games
    GROUP BY game_id
    ORDER BY popularity DESC
    LIMIT 5;
    

    Edit
    Somebody didn’t like this answer, so a little more context may be in order…

    You could do what you want without changing your schema. Your query would look similar to the following:

    SELECT game_id, count( game_id ) AS Popularity
    FROM (
        SELECT id, game1_id AS game_id
        FROM user
        UNION SELECT id, game2_id
        FROM user
        UNION SELECT id, game3_id
        FROM user
        ) AS user_games
    GROUP BY game_id
    ORDER BY Popularity DESC;
    

    However, the example in the question shows that you are using CodeIgniter’s ActiveRecord, which does not support UNION. So you would end up with a more complicated query and some extra hacking around in your method. Not exactly desirable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Site for writers and readers, both groups will be non-technical users (writers will be
Site A will be generating a set of records. Nightly they will backup their
The site I'm currently working on has a series of videos on a carousel
Test Site Example: http://cincinnati-website-design.net/sandbox/ I am using the Avada Theme and can't seem to
My site currently implements custom 404 pages which we have mapped in IIS. So
The site I'm working on is using the Frontpage module to redirect anon users
The subject of how to store web site users passwords in tables has come
I'm about to launch a site and got to the browser testing phase, then
Site loads fine in IE8,9,FF & Chrome. I can't seem to remedy the issue
Site sells downloadable media. How would one protect the download links so someone can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.