Iznogud
Iznogud

Reputation: 73

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.

So far, Im getting this :

enter image description here

But I need something like this to be more precise:

enter image description here

Upvotes: 0

Views: 104

Answers (3)

dhavald
dhavald

Reputation: 554

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;
    }
    ...
}

Upvotes: 0

lonewolf217
lonewolf217

Reputation: 629

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.

Upvotes: 1

Marc B
Marc B

Reputation: 360602

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.

Upvotes: 1

Related Questions