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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:43:01+00:00 2026-05-23T06:43:01+00:00

I have two tables: TRIPS —————– tripID | clientID and LEGS ——————————– legID |

  • 0

I have two tables:

      TRIPS
-----------------
tripID | clientID

and

              LEGS
--------------------------------
legID | depart | arrive | tripID

TRIPS has a one-to-many relationship with LEGS in that there are several legID‘s per tripID. I need to display them in the following format:

Trip tripID1:
    Leg legID1: depart1 - arrive1
    Leg legID2: depart2 - arrive2

Trip tripID2:
    Leg legID3: depart3 - arrive3
    Leg legID4: depart4 - arrive4

etc...

I’ve been able to iterate through the legIDs via a WHILE() loop, but I’m having trouble embedding a LEGS loop inside the TRIPS loop. My query is:

<?php
$legsQuery = "SELECT  trips.tripID, legs.depart, legs.arrive FROM legs, trips WHERE `trips`.tripID = `legs`.tripID";
$legsQueryResult = mysql_query($legsQuery) or die("QUERY LEG ERROR: " . mysql_error());
while($row = mysql_fetch_assoc($legsQueryResult)) {
    print_r($row);
}
?>
  • 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-23T06:43:01+00:00Added an answer on May 23, 2026 at 6:43 am
    1. add order by clause to sort by trip ID
    2. create $lastTripID variable to check when you get “legs” from “new trip”
    3. [recommended] use join to select data from multiple tables

    Code:

    <?php
        $legsQuery = "
            select  
                trips.tripID, 
                legs.depart, 
                legs.arrive 
            from 
                legs
                inner join trips on trips.tripID = legs.tripID
            order by
                trips.tripID
        ";
        $legsQueryResult = mysql_query($legsQuery) or die("QUERY LEG ERROR: " . mysql_error());
        $lastTripID = null;
        while ($row = mysql_fetch_assoc($legsQueryResult)) {
            if ( $row['tripID'] !== $lastTripID ) {
                echo $row['tripID'], "\n";
                $lastTripID = $row['tripID'];
            }
            print_r($row);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have two tables; Drivers keyed by DriverId and Trips with foreign keys
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables, testInstance and bugzilla that are associated by a third one,
I have an Access database that has two tables that are related by PK/FK.
hope this makes sense. I have merged together two tables one containing information about
I have two tables. One contains information on hotels, added to my website by
I have two tables. One contains information on hotels, added to my website by
I have two tables - one called customer_records and another called customer_actions . customer_records
I have the same trigger on two tables. The first one: CREATE TRIGGER updateURL1
I have two tables, one for bookings and one for room allocations. Booking table

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.