Link to home
Start Free TrialLog in
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

ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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...