Variable with different values for each while return

Asked

Viewed 31 times

-2

My problem is that the $buy variable has different values with each while return, and I don’t know how I select which one is the smallest

  while($dados=mysqli_fetch_array($result)) {

        $idenc = $dados["id"];
        $produto = $dados["produto"];
        $qtd = $dados["qtd"];
        $fornecedor = $dados["fornecedor"];

        $compra = $dados["compra"];


         echo "<h3><tr>"
            . "<td><center>$qtd</center></h3></td>"
            . "<td><center>$produto</center></h3></td>"

            . "<td><center>$compra</center></h3></td>"

            . "<td><center>$fornecedor</center></h3></td>"
            . "<td><form method='post' action='operacoesenc.php'>"
            . "<input type='hidden' name='idenc' value='$idenc'>"
            . "<input type='text' name='compra' value='$compra'>"
            . "<input type='hidden' name='operacao' value='excluir'>"
            . "<input type='submit' value='Cancelar'></h3>"
            . "</form>";
    }
  • The $buy variable pulls the sql data and, as there is more than one product in the table, it is different with each While loop. I wanted to highlight the line where the $buy amount is lower, but I don’t know how to use min() and I don’t even know if this is the right way to do what I want. Somebody help me ?

  • Post your SQL command so we can guide you.

1 answer

1


I don’t quite understand what you want, but if you are just going to sort your list from the lowest value of the $buy variable to the highest one just give an ORDER BY ascName at the end of your SQL query.

For example:

SELECT produto_nome as produto, produto_preco as preco FROM produtos ORDER BY produto_preco ASC

Return:

 +---------+-------+
 | produto | preco | 
 +---------+-------+
 | mouse   | 15    | 
 | teclado | 20    | 
 | fone    | 70    | 
 | monitor | 200   | 
 +---------+-------+

MIN() is used if you want to show only the least value record, that is, it does not return a list of records. Then it would be illogical for you to use a loop.

For example:

SELECT produto_nome as produto, MIN(produto_preco) as preco FROM produtos

Return:

 +---------+-------+
 | produto | preco | 
 +---------+-------+
 | mouse   | 15    | 
 +---------+-------+

Browser other questions tagged

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