Variable out of while is zero

Asked

Viewed 123 times

2

I make the total sum as follows in while:

$TotalSIva6 = 0;
$TotalSIva7 = 0;
$TotalCIva6 = 0;
$TotalCIva7 = 0;
while($rows_cursos3 = mysqli_fetch_array($result3)) {

    $teste24 = $rows_cursos3['Valor'];
    $teste25 = $rows_cursos3['Valor1'];

    $TotalSIva6 = $TotalSIva6 + $teste24;

    $TotalSIva7 = $TotalSIva7 + $teste25;

    $TotalCIva6 = $TotalCIva6 + $TotalSIva6;

    $TotalCIva7 = $TotalCIva7 + $TotalSIva7;

    $tabela4 .= '<tr>';  

    $tabela4 .= '<td>'.$rows_cursos3['Bolacha M DI'].'</td>';

    $tabela4 .= '<td>'.$rows_cursos3['Quantidade Bolacha'].'</td>';

    $tabela4 .= '<td>'.$rows_cursos3['Valor'].'</td>';

    $tabela4 .= '</tr>'; 

    $tabela4 .= '<tr>';  

    $tabela4 .= '<td>'.$rows_cursos3['Bolacha AS D'].'</td>';

    $tabela4 .= '<td>'.$rows_cursos3['Total Bolacha AS'].'</td>';

    $tabela4 .= '<td>'.$rows_cursos3['Valor1'].'</td>';

    $tabela4 .= '</tr>'; 

    $TotalSIva8 = 0;
    $TotalCIva8 = 0;
    while($rows_cursos5 = mysqli_fetch_array($result5)) {

        $teste26 = $rows_cursos5['Valor'];
        $teste27 = $rows_cursos5['Iva'];

        $TotalSIva8 = $TotalSIva8 + $teste26;

        $TotalCIva8 = $TotalCIva8 + $teste27;

        $tabela4 .= '<tr>';  

        $tabela4 .= '<tr>';  

        $tabela4 .= '<td>'.$rows_cursos5['ProdExtras'].'</td>';

        $tabela4 .= '<td>'.$rows_cursos5['Total'].'</td>';

        $tabela4 .= '<td>'.$rows_cursos5['Valor'].'</td>';

        $tabela4 .= '</tr>'; 

        $tabela4 .= '<tr>'; 

        $TotalSIva9 = 0;
        $TotalCIva9 = 0;
        while($rows_cursos4 = mysqli_fetch_array($result4)) {

            $teste28 = $rows_cursos4['Valor'];
            $teste29 = $rows_cursos4['Iva'];

            $TotalSIva9 = $TotalSIva9 + $teste28;

            $TotalCIva9 = $TotalCIva9 + $teste29;

            $tabela5 .= '<tr>';  

            $tabela5 .= '<td>'.$rows_cursos4['ProdExtras'].'</td>';

            $tabela5 .= '<td>'.$rows_cursos4['Total'].'</td>';

            $tabela5 .= '<td>'.$rows_cursos4['Valor'].'</td>';

            $tabela5 .= '</tr>'; 

            $tabela5 .= '<tr>'; 

        }
    }

$TotalSIva8 = $TotalSIva8 + $TotalSIva6 + $TotalSIva7;

$TotalCIva6 = $TotalCIva6*1.06;
$TotalCIva7 = $TotalCIva7*1.13;

$TotalCIva8 = $TotalCIva8 + $TotalCIva6 + $TotalCIva7;

$tabela4 .= '<tr>';
$tabela4 .= '<td colspan="3" style="text-align: right;">';
$tabela4 .= '<strong>Total S/Iva:</strong> '. $TotalSIva8 .'€';
$tabela4 .= '</td>';
$tabela4 .= '</tr>';

$tabela4 .= '<tr>';
$tabela4 .= '<td colspan="3" style="text-align: right;">';
$tabela4 .= '<strong>Total C/Iva:</strong> '. number_format($TotalCIva8, 2 , ',', ' ') .'€';
$tabela4 .= '</td>';

$tabela4 .= '</tr>';

$tabela4 .='</tbody>'; 

$tabela4 .= '</table>';

$tabela4 .= '</div>';
$tabela5 .= '<tr>';
$tabela5 .= '<td colspan="3" style="text-align: right;">';
$tabela5 .= '<strong>Total S/Iva:</strong> '. $TotalSIva9 .'€';
$tabela5 .= '</td>';
$tabela5 .= '</tr>';

$tabela5 .= '<tr>';
$tabela5 .= '<td colspan="3" style="text-align: right;">';
$tabela5 .= '<strong>Total C/Iva:</strong> '. number_format($TotalCIva9, 2 , ',', ' ') .'€';
$tabela5 .= '</td>';

$tabela5 .= '</tr>';

$tabela5 .='</tbody>'; 

$tabela5 .= '</table>';

$tabela5 .= '</div>';

echo $tabela5;

In the $tabela5 within the while before the line $tabela5 .= '<tr>'; if made:

var_dump ($TotalSIva9); var_dump ($TotalCIva9);

It returns the correct values:

(float(1) float(1.06) float(136.84) float(154.5592))

If you make the var_dump(); of variables outside the while. He returns:

(int(0) int(0)).

What will be the reason?

  • The values Valor and Iva collected from BD are always type number?

  • The variables $TotalSIva9and $TotalCIva9 shall contain only the sum of the values of $teste28 and $teste29 concerning the last iteration of while outermost ($rows_cursos5) because the two variables are receiving the zero value before starting the second while. Perhaps when the last iteration of whileexternal values of the while inner most are null.

1 answer

-1

mysqli_fetch_array is a php function, that is to say that the variables used within a function are declared in the first use, what is missing in your code is to make a reference using & pro compiler know that the variable within the function is the same as Voce stated before.

Try to do the following:

mysqli_fetch_array($result4, &$TotalCIva9)
{....}

you can study more in: http://php.net/manual/en/language.references.whatare.php

  • Didn’t work...

  • make direct reference in the body of the use function & in all $Totalsiva9.

  • Every time I make the reference when updating the page gives error 500.

  • @Jean Freitas The second parameter of mysqli_fetch_array is a constant to indicate the type of structure that will be used to store the collected BD data, as indicated by its documentation at http://php.net/manual/en/mysqli-result.fetch-array.php You may have confused with another function.

Browser other questions tagged

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