簡體   English   中英

在循環內只回應一次

[英]Echoing only once inside a loop

我有一點難題我無法破解。 我有動態表循環:

echo "<table border='1' padding='2' cellspacing='0' ";
echo "<tr> <th>ID</th> <th>Producto</th> <th>Ud/Caja</th> <th>Formato</th>  <th>Cajas</th> <th>Sueltas</th> <th>Total</th> </tr>";
// 
while($row = mysql_fetch_array( $result )) {

    // trying to a title line 
    if ($row['proveedor']=="Campocerrado" ) 
    { 

    echo "<tr> <th>TEST</th> <th>TEST</th> <th>TEST</th> <th>TEST</th>  <th>TEST</th> <th>TEST</th> <th>TEST</th> </tr>";


     ;
    ;}

    //

    echo "<tr>";
    echo  "<td>" . $row['idItem'] . "</td>";
    echo '<input type="hidden" name="hiddenidItem[]" id="hiddenField" value="'. $row['idItem'] .'">'; 

    echo "<td>" .  $row['producto'] . "</td>";
    echo '<input type="hidden" name="hiddenProducto[]" id="hiddenField" value="'. $row['producto'] .'">';

    echo '<input type="hidden" name="proveedor[]" id="lastValue" value="'. $row['proveedor'] .'">'; 

    echo "<td>" .  $row['ud/caja'] . "</td>";
    echo '<input type="hidden" name="ud/caja[]" id="hiddenField" value="'. $row['ud/caja'] .'">';

    echo "<td>" .  $row['formato'] . "</td>";
    echo '<input type="hidden" name="formato[]" id="hiddenField" value="'. $row['formato'] .'">';

    echo '<td><input type="number" name="cajas[]" id="cajas[]" value=""></td>';
    echo '<td><input type="number" name="sueltas[]" id="sueltas[]" value=""></td>'; 
    echo '<td><input type="number" name="total[]" id="total" value=""></td>';

    echo '<input type="hidden" name="lastValue[]" id="lastValue" value="'. $row['total'] .'">';  
    echo '<input type="hidden" name="proveedor[]" id="lastValue" value="'. $row['proveedor'] .'">'; 


    echo "</tr>";



 }

echo "</table>";
echo "<br>";

我無法弄清楚的部分是:

if ($row['proveedor']=="Campocerrado" ) 
        { 

        echo "<tr> <th>TEST</th> <th>TEST</th> <th>TEST</th> <th>TEST</th>  <th>TEST</th> <th>TEST</th> <th>TEST</th> </tr>";


         ;
        ;} 

我正在嘗試做的是運行循環,直到它遇到一個特定的$row來顯示$row的html一次。

到目前為止,我得到了這個:

在此輸入圖像描述

但我需要這樣的東西更精確:

在此輸入圖像描述

在純粹最簡單的答案形式中,使用令牌。

$tokenhit = false;
if (!$tokenhit && $row['proveedor']=="Campocerrado" ) { 

   echo "<tr> <th>TEST</th> <th>TEST</th> <th>TEST</th> <th>TEST</th>  
   <th>TEST</th> <th>TEST</th> <th>TEST</th> </tr>";

   $tokenhit = true;


}

但我不確定這是否回答了你的真實問題。

設置一個標志,表示您已經輸出了額外的行:

$show_extra_row = true;
while(...) {
   if (($row['proveedor']=="Campocerrado" ) && $show_extra_row) {
       $show_extra_row = false;
       echo ...
   }
}

在第一次顯示之后,標志變為false,並且永遠不會再輸出額外的行。

您可以使用臨時變量來檢查之前是否已滿足條件。

$firstTime = true;
while($row = mysql_fetch_array( $result )) {

    // trying to a title line 
    if ($firstTime && $row['proveedor']=="Campocerrado" ) 
    { 
        echo "<tr> <th>TEST</th> <th>TEST</th> <th>TEST</th> <th>TEST</th>  <th>TEST</th<th>TEST</th> <th>TEST</th> </tr>";
        $firstTime = false;
    }
    ...
}

暫無
暫無

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

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