无法阻止while循环重复

Working on invoice module which has two different tables master_table and detail_table. Know i want to display master_table's data above the table then in the table structure showing data of detail_table for each three rows header.

My code looks this.

$n = 0;
            $htmlpage = 1;
            $query = $dbConnection->prepare("SELECT * FROM master WHERE (DocNo BETWEEN ? AND ?) ORDER BY DocNo ASC");
            $query->execute(array($fdcu,$tdcu));
            while($rows = $query->fetch())
            {
                $n++;
                $noInvs = $rows['No_of_Inv'];
                $dno = $rows['DocNo'];
                $name = $rows['Customer_Name'];

Step-1: Now i'm pulling client data from another table

                $qry = $dbConnection->prepare("SELECT * FROM client WHERE client_name=?");
                $qry->execute(array($name));
                $row = $qry->fetch();
                $add = $row['address'];
                $city = $row['city'];

$proj = $rows['Project'];
                $projNo = $rows['Project_No'];
                $sermnth = $rows['Service_Month'];
                $grandtotal = $rows['Total_Amt'];
                $rupee = number2word($grandtotal);
                $index = 1;
                $ratetot = 0;
                $subtotal = 0;
                $lastPage = ceil($noInvs/3);
                $pageCount = 0;

Step-2: Now check the loop iteration

                while($index <= $noInvs)
                { 
                $cnt = 1;

                $body = "<div class='main-container'><table width='100%' style='padding:0 0 0 0;' align='center' cellpadding='0' cellspacing='0'>
                                <tr>
                                    <td valign='top' align='center' style='border:1px solid #000;'>
                                        <strong style='font-size:30px;'>Data</strong>
                                    </td>
                                </tr>


                                <tr>
                                    <td valign='top' align='left' style='padding: 0 0px 0 0px;border-left: solid 1px #000000;border-right: solid 1px #000000;border-bottom: solid 1px #000000;'>
                                        <table width='100%' align='left' cellpadding='0' cellspacing='0'>
                                            <tr>
                                                <td valign='top' style='width:50%; padding:5px; font-size:13px; border-right: solid 1px #000000;text-align:left;'><strong>CUSTOMER:</strong> <br /><p style='font-size:12px;'>$name<br />$add, $city</p></td>
                                                <td valign='top' style='width:50%; font-size:13px;'>
                                                    <table width='100%' cellpadding='0' cellspacing='0'>
                                                        <tbody>
                                                            <tr>
                                                                <td style='border-right: solid 1px #000000;padding:5px;width:40%;font-size:12px;'><br />Invoice No:<br />Invoice Date:</td>
                                                                <td align='left' style='padding:5px;font-size:12px;'><br />$ino<br />$invdate</td>
                                                            </tr>
                                                        </tbody>
                                                    </table>
                                                </td>
                                            </tr>
                                        </table>    
                                    </td>
                                </tr>

                                <tr>
                                    <td valign='top' align='left' style='border-left: solid 1px #000000;border-right: solid 1px #000000;border-bottom: solid 1px #000000;'>
                                        <table width=100% cellpadding='0' cellspacing='0' >
                                            <tbody>
                                                <tr style='border-right:solid 1px #000000;border-bottom:solid 1px #000000;'>
                                                    <td style='width:5%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>SN.</td>
                                                    <td style='width:45%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Description of Services</td>
                                                    <td style='width:13%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Per</td>
                                                    <td style='width:10%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Quantity</td> 
                                                    <td style='width:12%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Rate</td>
                                                    <td style='width:15%;text-align:center; border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Amount in Rs.</td>
                                                </tr>";

Step-4: check the loop condition

                while(true)
                {

Step-5: checking the iteration count

                    if($cnt>3)
                    {
                        $pageCount++;
                        break;
                    }

Step-6: Pulling data from details table

                    $query1 = $dbConnection->prepare("SELECT * FROM details WHERE DocNo=? AND DocNO_Index=?");
                    $query1->execute(array($dno,$index));
                    $rowCnt = $query1->rowCount();
                    while($rows1 = $query1->fetch())
                    {
                        $desg = $rows1['Item'];
                        $des = $rows1['Description'];
                        $Quantity = $rows1['Quantity'];
                        $Rate = $rows1['Rate'];
                        $per = $rows1['Per'];
                        $gross = $rows1['Gross'];
                        $SERVICE_TAX = $rows1['SERVICE'];
                        $SERVICE_TAX = ($SERVICE_TAX==null)? 0.0 : $SERVICE_TAX;
                        $SB_Tax = $rows1['SB_Tax'];
                        $SB_Tax = ($SB_Tax==null)? 0.0 : $SB_Tax;
                        $Krishi_Kalyan_Cess = $rows1['Krishi_Kalyan_Cess'];
                        $Krishi_Kalyan_Cess = ($Krishi_Kalyan_Cess==null)? 0.0 : $Krishi_Kalyan_Cess;
                        $ratetot = $ratetot + $Rate;
                        $subtotal = $subtotal + $gross;

                        if($cnt == 1 && $index > 1)
                        {
                            $body.= "<tr><td style='width:5%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:45%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>Opening Balance</td><td style='width:13%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:10%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:12%;text-align:right;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($ratetot - $Rate, 2)." /-</td><td style='width:15%;text-align:right;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($subtotal-$gross, 2)." /-</td></tr>";
                        }
                        $body.= "<tr><td style='width:5%;height:90px;vertical-align:top;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>$index</td><td style='width:45%;height:90px;vertical-align:top; border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>";

                        if($proj!=null)
                        $body.= "$proj<br/>";

                        if($desg!=null)                     
                        $body.= "$desg<br/>";

                        if($des!=null)
                        $body.= "$des<br/>";

                        if($projNo!=null)
                        $body.= "$projNo<br/>";

                        if($sermnth!=null)
                        $body.= "$sermnth<br/>";

                        $body.= "</td><td style='width:13%;height:90px;vertical-align:top;text-align:center; border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>$per</td><td style='width:10%;height:90px;vertical-align:top; border-right:solid 1px #000000;padding:3px 5px 3px 5px;text-align:center;;font-size:12px;'>$Quantity</td><td style='width:12%;height:90px;vertical-align:top;text-align:right; border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($Rate,2)."/-</td><td style='width:15%;height:90px;vertical-align:top; padding:3px 5px 3px 5px; text-align:right;;font-size:12px;'>". number_format($gross,2)."/-</td></tr>";

                        if($cnt == 3 || $index == $noInvs)
                        {
                            $body.= "<tr><td style='width:5%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:45%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>";

                            if($index != $noInvs)
                            {
                                $body.= "Closing Balance";
                            }
                            $body.= "</td> <td style='width:13%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:10%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:12%;border-right:solid 1px #000;border-top:solid 1px #000;text-align:right;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($ratetot,2)."/-</td><td style='width:15%;border-top:solid 1px #000;text-align:right;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($subtotal,2)."/-</td></tr>";
                        }
                    }
                    $index++;
                    $cnt++;
                }
                $body .= "</tbody></table></td></tr>";      

                $stax = round($subtotal*($SERVICE_TAX/100),2);
                $sbc = round($subtotal*($SB_Tax/100),2);
                $kkc = round($subtotal*($Krishi_Kalyan_Cess/100),2);
                $grandtotal = round(($subtotal+$stax+$sbc+$kkc),2);
                $rupee = number2word($grandtotal);
                $body .="<tr><td valign='top' align='left' style='border-left: solid 1px #000000;border-right: solid 1px #000000;border-bottom: solid 1px #000000;;font-size:12px;'><table cellpadding='0' cellspacing='0' width='100%'><tr><td rowspan='2' style='width:50%;height:50px; border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>";

                if($pageCount == $lastPage)
                {
                    $body .= "<strong style='font-size:12px;'>Narration : </strong><br/>Amount in Words (Rupees):<br/>";
                } 
                $body .= "<br/>$rupee";

                $body .= "</td><td style='width:35%; border-bottom:solid 1px #000000;border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>";

                if($pageCount==$lastPage)
                {
                    $body .= "Sub Total : <br />Service Tax @ 14% <br />Swachh Bharat Cess @ 0.5% <br />Krishi Kalyan Cess @ 0.5% <br />";
                }

                $body .= "</td><td style='width:15%;text-align:right; border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;;font-size:12px;'>";

                if($pageCount==$lastPage)
                {
                    $body .= "". number_format($subtotal,2)."/-<br />".number_format($stax,2)."/-<br />".number_format($sbc,2)."/-<br />".number_format($kkc,2)."/-<br />";
                }

                $body .= "</td></tr><tr><td style='width:35%; border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>";

                if($pageCount==$lastPage)
                {
                    $body .= "Total Amount";
                }

                $body .= "</td><td style='width:15%;text-align:right; padding:3px 5px 3px 5px;font-size:12px;'>";           

                if($pageCount == $lastPage)
                {
                    $body .= number_format($grandtotal,2)."/-";
                }
                $body .= "</td></tr></table></td></tr><tr><td valign='top' align='left' style='border-left: solid 1px #000000;border-right: solid 1px #000000;border-bottom: solid 1px #000000;;font-size:12px;'><table width='100%' cellpadding='0' cellspacing='0'>
<tbody>
<tr>
<td style='width:50%; padding:3px 5px 3px 5px;border-right:1px solid #000;font-size:12px;'>
<strong>Thank you</strong>
</td>
<td valign='bottom' align='center' style='width:50%; text-align:center;font-size:12px;'>
<strong>AUTHORISED SIGNATORY</strong>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
<p>&nbsp;</p></div>";

                $body .= "<html><head><title>$ino</title><style type='text/css'>.main-container{display: block;} @media print{.main-container{display: block;}@page{size:portrait;margin:10px auto;padding:0}.main-container{page-break-after:always!important;}}</style><script language='javascript' type='text/javascript'>function PrintPage() { window.print(); } </script></head><body style='font-size: 12px ! important;'>$body</body></html><!-- onload='window.print()'-->";

                echo "$body";                   
            }
        }

Issue is loop iteration.

My code output like this enter image description here

Any suggestions on how to do this or to point me in the right direction would be greatly appreciated.

You're trying to re-use an active database connection. That doesn't work. You need two connections.

Specifically, you're in the process of reading a result set from a query on your connection,

   $query = $dbConnection->prepare("some query");
   $query->execute(array($fdcu,$tdcu));
   while($rows = $query->fetch()) {
       ...

when you issue another query on the same $dbConnection ...

       $qry = $dbConnection->prepare("another query");
       $qry->execute(array($name));

You Can't Do That™ while the first query is still active. Open up and use a different connection for the queries issued from inside your loop.