2012-07-29 57 views
1

我一直想弄清楚我是如何在PHP腳本中獲得無限循環的。我知道的自定義函數沒有任何while循環,只有兩個循環是0或更多。 (我曾經想過將它們結合起來,但我會等到我解決這個問題)PHP Infinate循環

的$查詢和$結果被傳遞的原因是這樣我就可以獲取物體,實際查詢是

$查詢= $ db->查詢(「SELECT UNIX_TIMESTAMP(Fitting Date),Retail Price,Patient ID,Marketing,Mono or Bi FROM fitting WHERE YEAR(Fitting Date)='」。$ _REQUEST ['year']。「'ORDER BY Fitting Date」);

function printcontent ($result,$query,$month) { 
    global $totalhearingaids, $totalincomeoverall; 
    $hearingaids = 0; 
    $totalincome = 0.00; 
    $monthdate = date(n,$result->{"Fitting Date"}); 
    echo '<div id="innercontent"><table>'. "\n"; 
    while ($monthdate = $month) 
    { 
    echo '<tr>' . "\n"; 
    echo '<td>' . unixtodate($result->{"Fitting Date"}). '</td><td>' . printname($result->{"Patient Id"}) . "</td><td>" . $result->{"Mono or Bi"} . "</td><td>" . $result->{"Retail Price"} . "</td><td>" . printmarketing($result->{"Marketing"}) . "</td><br />" . "\n"; 
    echo '</tr>' . "\n"; 
    $hearingaids += $result->{"Mono or Bi"}; 
    $totalincome += $result->{"Retail Price"}; 
    $result = $query->fetch_object(); 
    $monthdate = date(n,$result->{"Fitting Date"}); 
} 
$totalhearingaids += $hearingaids; 
$totalincomeoverall += $totalincome; 
echo '<br />' . "\n" . '<b id="yeartitle">Sum</b>' . '<tr>' . "\n" . '<td></td><td></td><td>' . $hearingaids . '</td><td>$' . $totalincome . '</td><br />' . "\n" . '</table></div><br />' . "\n"; 

}

這一個發生在碼的主要的 「功能」。

$month = 1; 
while ($month <= 12){ 
       echo '<i>' . date(M,$month) . '</i><br />' . "\n"; 
       printcontent($object,$query,$month); 
       $month += 1; 
      } 

回答

3
while ($monthdate = $month) 

Is設置到$monthdate$month值,它總是將是大於1的值,即true

function printcontent ($result,$query,$month) { 
    $monthdate = date(n,$result->{"Fitting Date"}); 
    // $monthdate is set to the value of $month, which results in a > 0 value (evaluates to true) 
    while ($monthdate = $month) { } 
} 

$month = 1; 
while ($month <= 12){ 
    printcontent($object,$query,$month); 
    $month += 1; 
} 

您的代碼具有相同的效果:

function printcontent ($result,$query,$month) { 
    while ($month) { } 
} 

printcontent(null, null, true); 

要防止無限循環的printcontent函數中,檢查以確保$一個月< = 12有作爲。

+0

我覺得這很簡單,看起來很傻。非常感謝你。我會盡我所能接受。 – Athetius 2012-07-29 21:06:04

+0

@Athetius發生在我們身上!高興能夠幫助:) – 2012-07-29 21:07:34