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 6785813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:10:20+00:00 2026-05-26T17:10:20+00:00

This is a really common use case but I’ve never known the best way

  • 0

This is a really common use case but I’ve never known the best way of doing it. Say I have two tables

team table
– id
– team name

players table
– id
– team_id
– player_name

Say in the output html I want to show headings of the teams, and underneath each team the respective players:

Team 1
– bob
– jon

Team 2
– ken
– jason

Previously I’ve always used a select statement for the teams, and while looping thru the results, do another select statment where the foreign key would equal the ID of the current row of the outer query. Is this a dumb, non-efficient way of doing it? What’s the standard way of doing something like this? I could use join but I’m guess I would need to add some logic to only show the team heading once. I’m using php+mysql.

  • 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-26T17:10:20+00:00Added an answer on May 26, 2026 at 5:10 pm

    Generally looping through one result set and querying for more info inside the loop isn’t a good idea. It’s a best to avoid database queries in loops if possible. As you suggest a JOIN would work here:

    SELECT team_name,player_name FROM team INNER JOIN players on team.id=players.team_id
    

    There are a number of ways you could process the results in PHP. You could loop through the results and add them to an array

    array(
        team1 => array(
            bob,
            jon
        ),
        team2 => array( etc
    

    You can then loop through the array and output the results how you want.
    Or you could just loop directly through the db results and output them as you go storing the team name and only outputting it when it changes. I’d recommend the former solution. It’s cleaner and abstracted better.

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

Sidebar

Related Questions

This really has my stumped today. I'm sure its simple, but... Here is my
This really seems like a bug to me, but perhaps some databinding gurus can
This really puzzled for hours, I searched all over the internet, but got no
Is this really the easiest way to do this? http://simplesamples.info/MFC/Clipboard.php
I'm in a need to optimize this really tiny, but pesky function. unsigned umod(int
Ok, so say you have a really big Range in ruby. I want to
This is a common case for a developer. You want to add a feature
Is there a good way to use VS's designer to help in the case
I'm working with ASP.NET MVC but this really applies to any MVC framework. I
This really, really urks me, so I hope that someone can give me a

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.