Save data in a variable of an array within a while?

0

I make a query to the Database where it brings me a certain initial numbers and some endings that are saved in the arrays $ initial and $ final. I go through these arrays with a while but I want that every time the array is traversed, the initial $ value is saved in a temporary variable "$ temp" in order to make a sum between the value of the previous path of the "$ temp" and the current value of the final $ array, but I still do not know how to save the previous value of an array in a variable.

$resultado = $temp + $final; 

my code in which I do the procedure is the following but it is necessary to implement the line of the sum of the variables

  <?php
    $consultaS = $conexion->query("SELECT * FROM generador2.'TALONARIO' WHERE 'inicial' > 1800000");
    while ($filaS = $consultaS->fetch_array()) {
        $inicial=$filaS['inicial'];
        $final=$filaS['final'];
        //$resultado= $inicial + $final;                               
        echo "<tbody id='tbody' class='tbody'><tr><td name='elemento[]'><font size=2>" . $inicial . "</font></td>" . "<td><font size=2>" . $final . "</font></td>" . "<td><font size=2>" . $filaS['usuarioasigna'] . "</font></td>" . "<td><font size=2>" . $filaS['fechaasigna'] . "</font></td>". "<td><font size=2>" . $filaS['cedula'] . "</font></td>". "<td><font size=2>" . $filaS['estado'] . "</font></td>". "<td><font size=2>" . $filaS['recibido'] . "</font></td>". "<td><font size=2>" . $filaS['fecha_rec'] . "</font></td>". "<td><font size=2>" . $filaS['usuario_rec'] . "</font></td>" . "</tr>";                                           
    }              
?>
    
asked by Edgar Yezid Cruz 01.08.2018 в 18:49
source

2 answers

1

I think you should initialize your $ temp variable before the while loop, like this:

$temp = 0;

Then, within the cycle, you will add what you want to add (which was not completely clear to me), for example:

$temp += $inicial

This way you should have no problem adding something to this variable and keeping it at the end of the cycle.

    
answered by 01.08.2018 в 19:30
0

You must use the combined assignment operator +=

Official PHP documentation: link

...
while ($filaS = $consultaS->fetch_array()) {
    $inicial = $filaS['inicial'];
    $final   = $filaS['final'];

    $resultado += $inicial + $final;
   ...
}
    
answered by 01.08.2018 в 21:04