Why do accumulatives accumulate in a cycle that must be presented separately in PHP?

7

I have a table that generates records with different values, for each record there will be a value called atenuante which represents a selection made with checkbox .

These values are saved as a string de la forma 1-2 and in another table I have a value that corresponds to each option, being something like:

Atenuante: 1 | Valor: 0.2
Atenuante: 2 | Valor: 0.15
Atenuante: 3 | Valor: 0.25

In the system I make a explode to separate the string atenuante like this: 1 y 2 and then look up its value in another table. In order to obtain a summation to then show its value:

1= 0.2 | 2= 0.15 | total = 0.35
  

I happen to be calculating the sum well, but take that total and then add it to the total of the next record, something like this:

Registro 1 | total= 0.35
Registro 2 | total= (total real= 0.25) muestra: 0.60
Registro 3 | total= (total real= 0.36) muestra: 0.96
Registro 4 | total= (total real= 0.10) muestra: 1.06

I would like each value to be calculated separately and thus be shown, without accumulating:

Registro 1 | total= 0.35
Registro 2 | total= 0.25
Registro 3 | total= 0.36
Registro 4 | total= 0.10

This is the PHP code:

<?php
$query = "SELECT c_atenuante FROM sancionados";
$resultado = mysql_query($query) OR DIE("La consulta $query fallo" . mysql_error());

while ($registros = mysql_fetch_array($resultado)) //SI EXISTEN REGISTROS

    {
    $atenuantes = $registros["c_atenuante"];
    echo "<td>" . $atenuantes . "</td>";
    $atenuantes_explode = explode("-", $atenuantes);
    $cantidad_atenuantes = count($atenuantes_explode);
    for ($i = 0; $i < $cantidad_atenuantes; $i++)
        {
        $atenuante_seleccionado = $atenuantes_explode[$i] * 1;
        $query3 = "SELECT * from causas_nuevas2 where c_articulo=40 and c_aparte='$atenuante_seleccionado'";
        $resultado3 = mysql_query($query3) OR DIE("La consulta $query3 fallo" . mysql_error());
        if ($registros3 = mysql_fetch_array($resultado3))
            {
            $valor_atenuante = $registros3["valor"];
            $total_circunstancias1 = $total_circunstancias1 + $valor_atenuante;
            }
        }

    echo "<td>" . $total_circunstancias1 . "</td>";
    echo "</tr>";
    }

?>

NOTE:

  • Check the count and each record shows the amount: 1 , 2, 2, 1, 2, 1. .

  • Verify the value obtained, and do it well: 1=0.2, 2=0.3: 3=0.15 .

  • I multiply by 1, since the table where the values are is an integer, and the explode generates string.

asked by Victor Alvarado 26.04.2017 в 17:02
source

2 answers

7

You must reset your varaible $total_circunstancias1 = 0 just before your for remember that you are within While of all your records then when you change the record keeps the sum of the previous record.

It would be something like this:

<?php
$query = "SELECT c_atenuante FROM sancionados";
$resultado = mysql_query($query) OR DIE("La consulta $query fallo" . mysql_error());

while ($registros = mysql_fetch_array($resultado)) //SI EXISTEN REGISTROS

    {
    $atenuantes = $registros["c_atenuante"];
    echo "<td>" . $atenuantes . "</td>";
    $atenuantes_explode = explode("-", $atenuantes);
    $cantidad_atenuantes = count($atenuantes_explode);
    $total_circunstancias1 = 0;
    for ($i = 0; $i < $cantidad_atenuantes; $i++)
        {
        $atenuante_seleccionado = $atenuantes_explode[$i] * 1;
        $query3 = "SELECT * from causas_nuevas2 where c_articulo=40 and c_aparte='$atenuante_seleccionado'";
        $resultado3 = mysql_query($query3) OR DIE("La consulta $query3 fallo" . mysql_error());
        if ($registros3 = mysql_fetch_array($resultado3))
            {
            $valor_atenuante = $registros3["valor"];
            $total_circunstancias1 = $total_circunstancias1 + $valor_atenuante;
            }
        }

    echo "<td>" . $total_circunstancias1 . "</td>";
    echo "</tr>";
    }

?>
    
answered by 26.04.2017 / 17:20
source
6

The error seems to have to do with the line:

$total_circunstancias1 = $total_circunstancias1 + $valor_atenuante;

Well, in this variable you are accumulating in each cycle while , and apparently you want it to accumulate but only within the cycle for . Then, before finishing the cycle while (in your example, after echo '</tr>; ) you must add the line:

unset($total_circunstancias1);
    
answered by 26.04.2017 в 17:20