Avatar of David Schure
David Schure asked on

Running two "While Loops" from a query

I have a select query that I am running.  There are two while loops.  For whatever reason I can only get one at a time to return the data.  Either one works seperately.

$sql = "SELECT  s.session_id,
        s.session_date,
        s.session_type,
        CASE session_type
            WHEN 'Individual' THEN
                t.therapist_indy_fee
            WHEN 'Couples' THEN
                t.therapist_couple_fee
            WHEN 'Group' THEN
                t.therapist_group_fee
        END AS session_fee,
        s.session_status,
        t.therapist_name,
        t.therapist_street,
        t.therapist_city,
        t.therapist_state,
        t.therapist_zip_code,
        t.therapist_indy_fee,
        t.therapist_couple_fee,
        t.therapist_group_fee,
        c.client_name
FROM    tbl_session s
    INNER JOIN tbl_therapist t ON s.therapist_id = t.therapist_id
    INNER JOIN tbl_client c ON s.client_id = c.client_id
WHERE s.session_status = 'Completed'";
$result = $mysqli->query($sql);
$mysqli->close(); 
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Invoice</title>
<link rel="stylesheet" type="text/css"
    href="assets/css/style.css" />

    <meta charset="UTF-8">
    <title>Invoice</title>
    <!-- CSS FOR STYLING THE PAGE -->
     
</head>
  
<body>
    <section>
        <!--div><img src="../resources/images/Logos/CapLogo.png" alt="Logo"></div-->
        <h1>INVOICE</h1>
        <div class="from">
      <table>
            <tr>
                <th>Bill from:</th>
            </tr>
         <?php
                while($rows=$result->fetch_assoc())
                {
             ?>
            <tr>
                <td><?php echo $rows['therapist_name'];?><br>
                <?php echo $rows['therapist_street'];?><br>
                  <?php echo $rows['therapist_city'];?><br>                 <?php echo $rows['therapist_state'];?><br>                 <?php echo $rows['therapist_zip_code'];?></td>             </tr>             <?php                 }              ?>              </table></div>                  <!--div>Bill to:&nbsp;Arise</div-->         <!-- TABLE CONSTRUCTION-->         <div class="details">         <table>             <tr>                 <th>Session Date</th>                 <th>Client's Name</th>                 <th>Session Type</th>                 <th>Therapist Fee</th>             </tr>             <!-- PHP CODE TO FETCH DATA FROM ROWS-->             <?php   // LOOP TILL END OF DATA                  while($rows=$result->fetch_assoc())                 {              ?>             <tr>                 <!--FETCHING DATA FROM EACH                      ROW OF EVERY COLUMN-->                 <td><?php echo $rows['session_date'];?></td>                 <td><?php echo $rows['client_name'];?></td>                 <td><?php echo $rows['session_type'];?></td>                 <td><?php echo $rows['session_fee'];?></td>                              </tr>             <?php                 }              ?>         </table>         </div>

Open in new window

PHP

Avatar of undefined
Last Comment
David Favor

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ste5an

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
David Favor

Good question.

In the docs, there's no discussion of whether the $results object data is consumed or not.

Simple solution will be to clone off the data into an associative array in 1x while loop, so data is saved.

Then process the array in any number of while loops.
David Favor

As @ste5an mentioned, your code... with the nested while loops seems odd...

Seems like this won't produce the results you expect... as the code above builds a table within another table, with duplicate values, so unlikely this result is actually useful.

Look closely at your code to determine if this is really what you're trying to accomplish...
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes