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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:40:43+00:00 2026-06-03T15:40:43+00:00

I constantly am using stackoverflow to help me but right now I am stumped

  • 0

I constantly am using stackoverflow to help me but right now I am stumped – hopefully I can get some guidance on this issue:

I have two tables, one containing NFL Divisions the other containing NFL Teams. They are connected via the division_id field (primary key on Divisions table, foreign key on the Teams table).

What I would like to do is join the tables in a query and output the Teams by Division. I can accomplish this in PHP with two separate queries/loops but I want to know how/if I can accomplish this by joining the two tables in a single query instead.

Here is what I’ve got:

$query = "SELECT Divisions.division_id, Divisions.conference, Divisions.name, 
    Teams.name FROM Divisions INNER JOIN Teams ON 
    Teams.division_id = Divisions.division_id ORDER BY Divisions.division_id";

if( $result = $db->query($query) ){
  while( $row = $result->fetch_object() ){
    // here is where I'm stuck
  }
}

I’m trying to get the final output to look similar to:

AFC North
Cleveland Browns
Pittsburgh Steelers
Baltimore Ravens
Cincinnati Bengals

AFC East
Buffalo Bills
Miami Dolphins
etc..

Major thank you in advance to anyone who can help me out with this! I haven’t done any programming in over 2 years and just getting back into it.

  • 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-03T15:40:44+00:00Added an answer on June 3, 2026 at 3:40 pm

    Loop through the rows and output a new header whenever the division_id changes:

    $query = "SELECT Divisions.division_id, Divisions.conference, Divisions.name, 
        Teams.name AS teamName FROM Divisions INNER JOIN Teams ON 
        Teams.division_id = Divisions.division_id ORDER BY Divisions.division_id";
    
    if ($result = $db->query($query)) {
        $division = "";
        while ($row = $result->fetch_object()) {
            if ($division != $row["division_id"])
            {
                $division = $row["division_id"];
                echo "<br /><h2>{$row["conference"]}</h2>";
            }
    
            echo "{$row["teamName"]}<br />";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Python, I'm constantly using the following sequence to get an integer value from
Until now I'm using a SoftReference Cache for images in Android. This cache is
I'm constantly using the Implement interface shortcut in Visual Studio 2008. My problem is
I'm using OCamlWinPlus v1.9RC4. It sucks pretty hardcore. It's constantly crashing, and lacks basic
I'd like to initialize some top-level constants using float::consts::pi . For example: import float::consts::pi;
There's an external command I'm using constantly - basically launching a batch file. I'd
I constantly find myself sending .NET code snippets to other developers using MS outlook
This is one of the minor CSS problems that plague me constantly. How do
I'm using C and trying to get access to the constant M_PI (3.14159...). I
I'm currently using a compiler that constantly returns warnings, I don't want to see

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.