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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:52:02+00:00 2026-05-31T23:52:02+00:00

I have a PHP/MySQL query that returns to an HTML table, and I’m stuck

  • 0

I have a PHP/MySQL query that returns to an HTML table, and I’m stuck on a part where I need to make a second while loop in that query. I’m not sure where to go from here. I’ve tried a couple of different ways.

I want it to loop and give me the first set of data, then use the “Order_ID” and get a second set of data and put that second set in the first loop, then do it again.

Here’s what I have…

<?php
    $arrayLC = array();

    $OrdersToShip = mysql_query("
        SELECT *
        FROM Orders o
        WHERE o.LotCoded = 0 ORDER BY o.Order_ID");

    if ($OrdersToShip) {
        while ($info = mysql_fetch_array($OrdersToShip))
        {
            $Order_ID = $info['Order_ID'];
            $Customer_ID = $info['Customer_ID'];
            $OrderDate = $info['OrderDate'];

            $lotCodes = mysql_query("SELECT lotCode, Qty FROM `OrdersLotCodes` WHERE `Order_ID` = '".$Order_ID."'");

            if($lotCodes) {
                while ($info = mysql_fetch_array($lotCodes))
                {
                    $lotCode = $info['lotCode'];
                    $Qty = $info['Qty'];
                    array_push($arrayLC, $lotCode, $Qty);
                }
            }

            echo '<tr class="OLine">
                 <td><input type="button" class="button viewThis" value="VIEW"></td>
                 <td>'.$Order_ID.'</td>
                 <td>'.$Customer_ID.'</td>
                 <td>'.$OrderDate.'</td>
                 <td>'.print_r($arrayLC).'</td>
                 </tr>';
        }
    }
    else {
        echo "encountered an error.".mysql_error();
    }
    mysql_close($conn);
?>

What am I missing? What should I do?

::EDIT::

I’ve changed the mysql_query to:

SELECT o.Order_ID, o.Customer_ID, o.OrderDate, olc.lotCode, olc.qty
FROM Orders o
LEFT JOIN OrdersLotCodes olc ON o.Order_ID = olc.Order_ID
WHERE o.LotCoded = 0 ORDER BY o.Order_ID

Now, how would I take the output with the OrderLotCodes and put them into an array to be printed in the table? How would I put them in an array then bring the related one by Order_ID?

  • 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-31T23:52:03+00:00Added an answer on May 31, 2026 at 11:52 pm

    One problem that I can see is that you are not resetting $arrayLC inside the outer while loop; therefore, the codes from each order get appended to those of the previous one, ad infinitum.

    if($OrdersToShip) {
        while ($info = mysql_fetch_array($OrdersToShip)) {
    
        $arrayLC = array(); // SHOULD BE IN HERE!
    

    Apart from that, when you get this code working you should think about the fact that for N orders, this code executes N + 1 queries (1 to get the orders and one per order to get the items). This is a really inefficient way to do things considering that you can retrieve the same information with just one query if you perform a left outer join. So your next stop should be reading up on SQL joins and how they can help you retrieve associated data.

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

Sidebar

Related Questions

I have a php script that runs a mysql query, then loops the result,
I need to build a MySQL query that returns only the common columns for
I have a php file that receives data from mySQL table. The mySQL table
I have the following mysql query which I am running with php like so.
I have a query like this (Mysql 5.X, PHP - formatted for legibility) $query
We have a PHP/MYSQL application that collects user input, including special characters like ø,ü,ñ,
I have a legacy PHP/MySQL app that calls mysql_connect(). Tons of existing downstream code
i work with php/Mysql i have table with two cols (date , cash) with
I currently have a news website setup in PHP/MYSQL that's a bit old and
I have a php page that pulls data from a mysql database based on

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.