无法阻止while循环重复

时间:2017-01-21 14:57:39

标签: php mysql while-loop

处理发票模块,该模块有两个不同的表master_table和detail_table。知道我想在表格上方显示master_table的数据,然后在表格结构中显示每个三行标题的detail_table数据。

我的代码看起来就是这样。

$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'];

步骤1:现在我从另一个表中提取客户端数据

                $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;

步骤2:现在检查循环迭代

                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>";

步骤4:检查循环条件

                while(true)
                {

步骤5:检查迭代次数

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

步骤6:从详细信息表中提取数据

                    $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";                   
            }
        }

问题是循环迭代。

我的代码输出如enter image description here

对于如何做到这一点或指出我正确方向的任何建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

您正在尝试重新使用活动数据库连接。这不起作用。你需要两个连接。

具体来说,您正在从连接上的查询中读取结果集,

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

当您在同一$dbConnection ...

上发出另一个查询时
       $qry = $dbConnection->prepare("another query");
       $qry->execute(array($name));

当第一个查询仍处于活动状态时,你无法做到这一点。打开并为循环内部发出的查询使用不同的连接。