Determine when Loop WHILE should stop PHP

Asked

Viewed 101 times

0

I have the following problem,

I have a system in PHP that makes a WHILE in a column of the database that contains certain values and I need WHILE to stop at a value that I determine.

The first column with the ID’s and the second with the values, I need my WHILE to perform any function until the total value of the sums of the quantities is less than or equal to 850 and then stop.

Ex:
inserir a descrição da imagem aqui

Code below:

$resultado = $PDO->query("SELECT * FROM teste");
//Variável 
$i=1;
//Percorrendo e exibir registros 
# Cria e abre o arquivo .txt para salvar os registros da consulta
$arquivo_total = '../imprimir/xereta.prn';
$fppp = fopen($arquivo_total, 'w');

//Percorrendo e exibir registros
while ($registro = $resultado->fetch(PDO::FETCH_ASSOC)) {
    $id = $registro['id'];
    $quantidade = ceil($registro['quantidade']*10/100 + ($registro['quantidade'])+20). "\r\n\n";
    $i++;
        # Escreve no arquivo .txt o registro atual da consulta
        # Início primeira etiqueta 

            fwrite($fppp, $quantidade);

        # Final Segunda etiqueta 
        } # << Fim do while        
        # Fecha o arquivo .txt
        fclose($fppp);      

Someone knows how to help me.

  • Why did you ask the question again? Had not already been fixed in https://answall.com/questions/461002/fazer-while-somar-at%C3%A9-determined-value-php? noredirect=1#comment875557_461002

  • Good morning @Leocaracciolo the problem was that the other topic was blocked and I ended up creating another one and forgot to delete, it was a failure on my part. But anyway thank you so much for the warning.

  • Got it!! When an answer solves your difficulty mark it (the best) as accepted, see https://i.stack.Imgur.com/evLUR.png

3 answers

0

I believe that a simple solution would be to create a variable that accumulates the sum in each iteration, and then you test the value of the variable to jump out of the loop...

$somatorio = $somatorio + $quantidade
if ($somatorio >= 850) break;

0

I consider that the connection with the BD is already done and that the records of the field 'quantity' are of type 'int'.

<?php
$stop = 850;
$query = $mysqli->query("SELECT * FROM teste"); //Consulta ao BD
$total = ""; //Criando a variável $total(vazia)
$registro = $query->fetch_row(); //Consulta percorre a linha
//Aqui entra a minha sugestão para seu problema, usando FOREACH
    foreach($registro as $valor){
        $a = $valor[1]; //$a recebe o primeiro valor
        $total += $a; //$a incorpora $total
        if($total <= $stop)
        break;
    }
?>

Hug.

0

That’s the same question that was closed duplicate and resolved by the comments

Place these lines at the start of while in your PHP

$soma += $registro['quantidade'];
 if($soma >850){
    break;
 }

Behold

//Percorrendo e exibir registros
while ($registro = $resultado->fetch(PDO::FETCH_ASSOC)) {
// break - se a soma for maior que 850 interrompe o loop
$soma += $registro['quantidade'];
 if($soma >850){
    break;
 }
 $id = $registro['id'];
 ........
 ........

break

Browser other questions tagged

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