Variable out of while is zero

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% with% in% of% before% line of% if you do:

$tabela5

It returns the correct values:

while

If you make the $tabela5 .= '<tr>'; of the variables out of var_dump ($TotalSIva9); var_dump ($TotalCIva9); . It returns:

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

What will be the reason?

    
asked by anonymous 17.05.2018 / 16:28

1 answer

-1

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

Try the following:

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

you can study more at: link

    
17.05.2018 / 16:52