When generating several FORM with WHILE the first FORM does not appear

0

What I try to do is a table that with the while is generating a div that has an image, h4 , two input and a button, where the input they go inside a form with method POST , when executing it the first field of the while does not answer me, but the other generated fields if they do what they should (an insertion to the BD).

PS: I generate everything with a query from the same DB

PD2: I realize that% is not form when inspecting the code with Chrome

<div class="container" >
<div class="row">
        <input type="hidden" class="form-control" id="MESA" name="MESA" value="<?php echo $MESA ?>">
                <div class="col-md-12">
                <!-- Nav tabs -->
                <div class="row">
                <ul class="nav nav-tabs" role="tablist">
                <li role="presentation" class="active"><a href="#cer" aria-controls="cer" role="tab" data-toggle="tab">Cerveza</a></li>
                <li role="presentation"><a href="#preparados" aria-controls="preparados" role="tab" data-toggle="tab">Preparados</a></li>
                <li role="presentation"><a href="#comida" aria-controls="comida" role="tab" data-toggle="tab">Comida</a></li>
                </ul>
                <!-- Tab panes -->
        <div class="tab-content">
        <div class="tab-pane fade in active" id="cer">
        <div class="row">

        <?php
        $x=0;
        while($reg=  mysqli_fetch_array($cerveza))
         {
        echo '<div class="col-md-3">';
        echo '<div class="thumbnail">';
        echo '<img src="../Img/'.mb_convert_encoding($reg['URL'], "UTF-8").'" width="160px" height="190px" >';

          echo "<div class='caption'>";
          echo '<h4>'.mb_convert_encoding($reg['PRODUCTO'], "UTF-8")." $".mb_convert_encoding($reg['PRECIO'], "UTF-8"). '.00</h4>';
          echo "<div class='input-group'>";
          echo '<form enctype="multipart/form-data" action="../Sql/Menu/agregar.php" method="POST">';

         echo '<input type="text" class="form-control" id="cantidad" name="cantidad" placeholder="NOA-NOA" onkeypress="return soloNumeros(event);"> 
          <input type="hidden" class="form-control" value='.$reg['ID_MENU'].' id="pedido" name="pedido" placeholder="NOA-NOA" onkeypress="return soloNumeros(event);"> ';
        echo '<input class="btn btn-success" type="submit">';
        echo '</form>';
        echo "</div>";
        echo "</div>";
        echo'</div>';
        echo'</div>';
        if($x==4)
         {
            echo'<br>';
            $x = 0;
         }
            $x++;
         }
         ?>
            </div>  
            </div>
    
asked by Oscar Velázquez 05.06.2018 в 09:03
source

1 answer

0

The problem is that the WHILE loop first checks if the condition is true and then executes the code. Using a DO WHILE, first execute the code and then verify if the condition is met. Something like this:

<?php
    $x=0;
    do

     {
    echo '<div class="col-md-3">';
    echo '<div class="thumbnail">';
    echo '<img src="../Img/'.mb_convert_encoding($reg['URL'], "UTF-8").'" width="160px" height="190px" >';

      echo "<div class='caption'>";
      echo '<h4>'.mb_convert_encoding($reg['PRODUCTO'], "UTF-8")." $".mb_convert_encoding($reg['PRECIO'], "UTF-8"). '.00</h4>';
      echo "<div class='input-group'>";
      echo '<form enctype="multipart/form-data" action="../Sql/Menu/agregar.php" method="POST">';

     echo '<input type="text" class="form-control" id="cantidad" name="cantidad" placeholder="NOA-NOA" onkeypress="return soloNumeros(event);"> 
      <input type="hidden" class="form-control" value='.$reg['ID_MENU'].' id="pedido" name="pedido" placeholder="NOA-NOA" onkeypress="return soloNumeros(event);"> ';
    echo '<input class="btn btn-success" type="submit">';
    echo '</form>';
    echo "</div>";
    echo "</div>";
    echo'</div>';
    echo'</div>';
    if($x==4)
     {
        echo'<br>';
        $x = 0;
     }
        $x++;
     }
     while($reg=  mysqli_fetch_array($cerveza))
 ?>
    
answered by 05.06.2018 в 16:41