在循环内只回应一次

时间:2014-02-03 16:18:24

标签: php

我有一点难题我无法破解。我有动态表循环:

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来显示行的html一次。

到目前为止,我得到了这个:

enter image description here

但是我需要这样的东西更精确:

enter image description here

3 个答案:

答案 0 :(得分:1)

以纯粹最简单的答案形式,使用令牌。

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


}

但我不确定这是否回答了你的真实问题。

答案 1 :(得分:1)

设置一个标志,表示您已经输出了额外的行:

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

第一次显示后,该标志变为false,额外的行将永远不再输出。

答案 2 :(得分:0)

你可以有一个临时变量来检查之前是否已满足条件。

$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;
    }
    ...
}
相关问题