简体   繁体   English

在循环内只回应一次

[英]Echoing only once inside a loop

i have bit of a puzzle Im unable to crack. 我有一点难题我无法破解。 I have dynamic table looping: 我有动态表循环:

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

The part I'm not able to figure out is this: 我无法弄清楚的部分是:

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


         ;
        ;} 

What I'm trying to do is run the loop until it encounters a specific $row to display the html for the row once. 我正在尝试做的是运行循环,直到它遇到一个特定的$row来显示$row的html一次。

So far, Im getting this : 到目前为止,我得到了这个:

在此输入图像描述

But I need something like this to be more precise: 但我需要这样的东西更精确:

在此输入图像描述

In the pure simplest form of an answer, use a token. 在纯粹最简单的答案形式中,使用令牌。

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


}

But i am not sure this answers your true question or not. 但我不确定这是否回答了你的真实问题。

set a flag to indicate that you've already output your extra row: 设置一个标志,表示您已经输出了额外的行:

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

After it's displayed for the first time, the flag becomes false and the extra row will never be output again. 在第一次显示之后,标志变为false,并且永远不会再输出额外的行。

you can have a temporary variable to check if the condition already met before. 您可以使用临时变量来检查之前是否已满足条件。

$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