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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:00:38+00:00 2026-05-25T00:00:38+00:00

I have several tables from which I need to display data. Schools ——- id

  • 0

I have several tables from which I need to display data.

Schools
-------
id | Title

Programs
--------
id | descr | title | type | school.id

Granted, not the best set up but there isn’t any thing I can do about the database structure.

I need to create a list, seperated by School, Program, and Program Type. Right now, I have a big huge nested loop (pseudo code):

Select * from Schools
For Each School
    print $school_header;
    Select Program Type from Programs WHERE School.id = $school_id 
        For Each Program type
           print $type_header;
           Select * from Programs where School.id = $school_id and type = $program_type
           For Each Program
               print $program_link;

Needless to say its a big mess.

The end result is a list:

  • Program 1
    • Program Type 1
      • Program
      • Program
    • Program Type 2
      • Program
      • Program
    • Program Type 3
      • Program

Is there a way to do this with fewer queries and less code that isn’t so database intensive?

  • 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-25T00:00:39+00:00Added an answer on May 25, 2026 at 12:00 am

    The real issue here is not whether one or the other programming language is more efficient. The vast, vast difference in performance is due to data locality, namely that executing the query on the database itself doesn’t require you to move all of the data back and forth over the wire for evaluation, whereas the PHP solution does.

    For your query, what you want to do is write a stored procedure which returns multiple result sets (for further joining in PHP) or a single large result set (by joining in SQL). My guess would be that it would look something like:

    SELECT S.Title as School_Title, pt.title as Program_type_title, p.title as Program_Title
    FROM School S 
    INNER JOIN ProgramType pt on s.id = pt.school_id
    INNER JOIN Program p on pt.id = p.program_type_id
    ORDER BY S.Title, pt.title, p.title
    

    This will give you the large rectangular array, ordered by each title in order of priority. This way you can loop through rows until you see the School title change, or the Program Type title change, or whatever, and render the appropriate closing tags.

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

Sidebar

Related Questions

I have several sparsely populated tables in an Excel workbook from which I need
I have to compute a value involving data from several tables. I was wondering
I have several tables whose only unique data is a uniqueidentifier (a Guid) column.
I have several sources of tables with personal data, like this: SOURCE 1 ID,
I have a data-upload function that load some data into several tables and processes
I have an Access database (in Access 2003) with several tables, and data must
I have several hosts from which i want to do the same query. So
I'm attempting to write a linq query which uses several tables of related data
I have two tables, in which one user can have several contacts: User(UserId, UserName)
I have several tables: CREATE TABLE [dbo].[Tracks]( [Id] [uniqueidentifier] NOT NULL, [Artist_Id] [uniqueidentifier] NOT

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.