create a select within while

Asked

Viewed 104 times

1

I have this consultation:

while($rows_cursos = mysqli_fetch_array($resultado_cursos)) {

$tabela1 .= '<tr>';

$tabela1 .= '<td style="font-size: 12px">'.$rows_cursos['DataAprovacao'].'</td>';

$tabela1 .= '<td style="font-size: 12px">'.$rows_cursos['IdTipoProduto'].'</td>';

$tabela1 .= '<td style="font-size: 12px">'.$rows_cursos['IdProduto'].'</td>';

$tabela1 .= '<td style="font-size: 12px"> '.$rows_cursos['Quantidade'].'</td>';

$tabela1 .= '<td style="font-size: 12px"> '.$rows_cursos['IdRequerente'].'</td>';

$tabela1 .= '<td style="font-size: 12px"> '.$rows_cursos['IdDestino'].'</td>';

$tabela1 .= '<td style="font-size: 12px"> '.$rows_cursos['Estado'].'</td>';

$tabela1 .= '<td> <select data-qtd3="Fornecedor" style="width:106px" name="Fornecedor[]" id="Fornecedor">
<option></option>
<?php        
         $sql = "SELECT * FROM Fornecedor ORDER BY Fornecedor ASC";
         $qr = mysqli_query($conn, $sql);
         while($ln = mysqli_fetch_assoc($qr)){
            echo '<option value="'.$ln['Id'].'">'.$ln['Fornecedor'].'</option>';
         }
      ?>        
</select></td>';

$tabela1 .= '</tr>';

}

Only if you select it

$tabela1 .= '<td> <select data-qtd3="Fornecedor" style="width:106px" name="Fornecedor[]" id="Fornecedor">
    <option></option>
    <?php        
             $sql = "SELECT * FROM Fornecedor ORDER BY Fornecedor ASC";
             $qr = mysqli_query($conn, $sql);
             while($ln = mysqli_fetch_assoc($qr)){
                echo '<option value="'.$ln['Id'].'">'.$ln['Fornecedor'].'</option>';
             }
          ?>        
    </select></td>';

not working. Does not return data from vendors I have in the database table

  • "Does not show the types of vendor I have in the database table", what exactly does that mean? It doesn’t return the data?

  • @Ricardo Pontual, yes I already edited the question

1 answer

2


Try it this way:

$tabela1 .= '<td> <select data-qtd3="Fornecedor" style="width:106px" name="Fornecedor[]" id="Fornecedor">';

$sql = "SELECT * FROM Fornecedor ORDER BY Fornecedor ASC";
$qr = mysqli_query($conn, $sql);
while($ln = mysqli_fetch_assoc($qr)){
    $tabela1 .= '<option value="'.$ln['Id'].'">'.$ln['Fornecedor'].'</option>';
}

$tabela1 .= '</select></td>';

The way you were doing you were adding one PHP with queries inside the string associated with variable $tabela1.

I imagine you were trying to get this snippet of PHP to run when the variable $tabela1 was printed. Instead, popular search options while mounting the table.

  • I got the result you wanted

Browser other questions tagged

You are not signed in. Login or sign up in order to post.