簡體   English   中英

PHP雙循環,第二個循環僅運行一次

[英]PHP double while loop, second loop is running only once

我想在第二個while循環的幫助下打印信息,但是它只運行一次。

while($nextDate<$currentDate)
{
        $nextDate=date('Y-m-d',strtotime('+ 6 days',strtotime($weekDate))); 

        $qM="select count(*) as count ,Deposit.DepositNo, Deposit.DepositDate,
                     sum(DepositItem.Amount) as Amount,DAmount
                     from DepositItem
                     inner join Deposit on Deposit.DepositNo=DepositItem.DepositNo
                     where Deposit.DepositDate>='".$weekDate."' and Deposit.DepositDate<='".$nextDate."' group by Deposit.DepositNo 
                     order by Deposit.DepositNo desc";

        $connM=new dbconfig();
        $connM->connectdb();
        $connM->execute($qM);
        $amt=0;
        $damt=0;
        while($rowsM =$connM->fetch_row()) 
        {
            $amt=$amt+$rowsM['Amount'];
            $damt=$damt+$rowsM['DAmount'];
        }
}

$ nextDate始終大於$ currentDate,因為您要添加6天

$nextDate=date('Y-m-d',strtotime('+ 6 days',strtotime($weekDate))); 

從您的代碼來看,有太多未知因素無法解決問題。

但是我在代碼中寫了一些注釋來指出潛在的問題/調試建議:

// what are the (expected) values of $nextDate and $currentDate ?
while ($nextDate < $currentDate) {
    $nextDate = date('Y-m-d', strtotime('+ 6 days', strtotime($weekDate)));
    // this makes the big loop either run once (if $weekDate is within the 6 days interval)
    // or an infinite loop if it isn't

    $qM =   "select count(*) as count ,Deposit.DepositNo, Deposit.DepositDate,
             sum(DepositItem.Amount) as Amount,DAmount
             from DepositItem
             inner join Deposit on Deposit.DepositNo=DepositItem.DepositNo
             where Deposit.DepositDate>='" . $weekDate . "' and Deposit.DepositDate<='" . $nextDate . "' group by Deposit.DepositNo 
             order by Deposit.DepositNo desc";
    // do an echo $qM and copy it in Phpmyadmin, see if it returns expected results

    $connM = new dbconfig(); // not quite good to have these inside a loop
    $connM->connectdb(); // you should put these 2 lines outside
    $connM->execute($qM);
    $amt = 0;
    $damt = 0;
    while ($rowsM = $connM->fetch_row()) { // do a var_dump($rowsM) inside the loop
        $amt = $amt + $rowsM['Amount'];
        $damt = $damt + $rowsM['DAmount'];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM