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

  • SEARCH
  • Home
  • 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 7731837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:34:50+00:00 2026-06-01T06:34:50+00:00

I am making simple connection between two table – first one called users has

  • 0

I am making simple connection between two table – first one called users has fields :

|| id || name ||

and the second table called groups has the same fields:

|| id || name ||

The relations b/w them is many to mane because one user can be in many groups(of interest) and a group will contain more than one user. So I have a third table user_group with fields:

|| user_id || group_id ||

Where the both are foregin keys one for users and one for groups.

The goal is to make a list with all the groups and the people participating in each one.
For this I made my query:

$q = $this->db->query('SELECT u.name  FROM users u 
JOIN user_group ug ON u.id = ug.user_id 
JOIN groups g ON g.id = ug.group_id WHERE ug.group_id = "4" ');

then modified it using active records:

$this->db->select('users.name');
$this->db->from('users');
$this->db->join('user_group', 'user_group.user_id = users.id');
$this->db->join('groups', 'groups.id = user_group.group_id');
$this->db->where('user_group.group_id', 3);
$q = $this->db->get();

And this way I can take the users for any group by given the ‘id’ from the group table. But what I can’t figure out is how to make it display both – the name of the group along with the users participating. When I create and delete from the tables the id’s become very unorder I may have 20 groups and some group may have id = 53 so jsut looping from 1 to number_of_groups is not good enough. What’s the way to do this?

Thanks
Leron

  • 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-06-01T06:34:51+00:00Added an answer on June 1, 2026 at 6:34 am

    You cannot get the group as well as the users in that group all in one query. You could get the concatenated user list for each group in one query

    SELECT g.group_name, GROUP_CONCAT(u.fullname) 
    FROM group g
    JOIN user_group ug ON g.id = ug.id
    JOIN user u ON ug.user_id = u.id
    GROUP BY g.id
    

    The group => user is Many to Many relation so a user in a group will return multiple rows for users.

    If you need the list of the user details as well and not the concatenated form.
    You can iterate over the group list and then add a key to the users.

    $groups = $this->db->get('groups')->result();
    
    foreach($group as &$g){
      $this->db->select('users.name');
      $this->db->from('users');
      $this->db->join('user_group', 'user_group.user_id = users.id');
      $this->db->where('user_group.group_id', $g->id);
      $g->users = $this->db->get()->result();
    }
    

    Now you loop through $group and can access the users with $group->users

    Notice that & before $g in the foreach loop. Since foreach operates over the copy of the variable being passed, you have pass the reference to it.

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

Sidebar

Related Questions

I'm making a simple program in Objective-C. It has one class with a lot
I am making a simple project where I take id and name from user
making a simple movie review site to practice PHP. on one page ( a
I am making an Android application. Since it is so simple, I first thought
Im making a simple alternative to the default linux system monitor. Im looking to
in making a simple cgi server for a course. To do that in some
I’m making a simple LOB app which loads data from an XML file and
I am making a simple multiplayer economic game in pygame. It consists of turns
I was making a simple class for a small project and decided to just
I am making a simple byte buffer that stores its data in a char

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.