adding cells with while in PHP grouped

1

I have a table drawn in PHP with records of a select in the database, and the drawing like this

      if(!is_null($plantilla)) : 
        $curp_prof = ' ';
        $hrs_base = 0;
        $hrs_comp = 0;
        $hrs_int = 0;
        $hrs_conf = 0;
        $hrs_mes = 0;
        $sumHoras = 0;
      while($i = mysqli_fetch_array($plantilla)) :
        if($curp_prof != $i[0] || $hrs_base != $i[1] || $hrs_comp != $i[2] ||$hrs_int != $i[3] || $hrs_conf != $i[4] || $hrs_mes != $i[5]){
      ?>
          <tr>
            <td>
            <a onclick="javascript:return confirm('¿Seguro que desea modificar?, al realizar esta opcion tendra que volver a asignar horas y materias');" href="coordinador.php?f=Eliminar&curp=<?php echo $i[14]; ?>">
              <center><i class="fa fa-pencil-square" aria-hidden="true"></i></center>
            </a>
            </td>
            <td><?php echo $i[0]; ?></td>
            <td bgcolor="#37831D"><center><?php echo $i[1]; ?></center></td>
            <td bgcolor="#37831D"><center><?php echo $i[2]; ?></center></td>
            <td bgcolor="#37831D"><center><?php echo $i[3]; ?></center></td>
            <td bgcolor="#37831D"><center><?php echo $i[4]; ?></center></td>
            <td><center><?php echo $i[5]; ?></center></td>
      <?php 
        $curp_prof  = $i[0];
        $hrs_base   = $i[1];
        $hrs_comp   = $i[2];
        $hrs_int    = $i[3];
        $hrs_conf   = $i[4];
        $hrs_mes    = $i[5];

        }else{

      ?>
        <td> </td>
        <td> </td>
        <td> </td>
        <td> </td>
        <td> </td>
        <td> </td>
        <td> </td>
      <?php 
        }
      ?>   
        <td><?php echo $i[6]; ?></td>
        <td><?php echo $i[7]; ?></td>
        <td><center><?php echo $i[8]; ?></center></td>
        <td><center><?php echo $i[9]; ?></center></td>
        <td bgcolor="#37831D"><center><?php echo $i[10]; ?></center></td>
        <td><center><?php echo $i[11]; ?></center></td>
        <td bgcolor="#37831D"><center><?php echo $i[12]; ?></center></td>
        <td bgcolor="#37831D"><?php echo $i[13]; ?></td>
      <?php 

      $sumHoras = $sumHoras + $i[13];
      ?>
        <td bgcolor="#37831D"><?php echo $sumHoras; ?></td>
        </tr>
      <?php
      endwhile;
      ?>
      <?php
      endif;
      ?>

Result in this table:

+-----------------------------------+----------+------------+------------+
| profesor $i[0];                   | materia  |   $i[13];  | $sumHoras  |
|                                   | $i[7];   | gran_total |       suma |
+-----------------------------------+----------+------------+------------+
| CRUZ CORONA ALEJANDRO             | 1        |      23.20 |      23.20 |
|                                   | 2        |      23.20 |      46.40 |
| GUILLÉN ALCÁNTARA ERNESTO         | 5        |      23.20 |      69.60 |
|                                   | 3        |      23.20 |      46.40 |
|                                   | 1        |      23.20 |      72.80 |
| MORALES ARREDONDO THANIA BERENICE | 6        |      23.20 |      96.00 |
|                                   | 7        |      23.20 |     119.20 |
|                                   | 8        |      23.20 |     142.40 |
|                                   | 1        |      23.20 |     165.60 |
| MORALES RESENDIZ JORGE            | 9        |      23.20 |     188.80 |

And I want to add the total cells and come out in sum, that is, for each teacher, because in the while it adds but all and I want it to do it for each teacher. SO THAT IT STAYS SO

+-----------------------------------+----------+------------+------------+
| profesor                          | materia  | gran_total |       suma |
+-----------------------------------+----------+------------+------------+
| CRUZ CORONA ALEJANDRO             | 1        |      23.20 |      23.20 |
|                                   | 2        |      23.20 |      46.40 |
| GUILLÉN ALCÁNTARA ERNESTO         | 5        |      23.20 |      23.20 |
|                                   | 3        |      23.20 |      46.40 |
|                                   | 1        |      23.20 |      69.60 |

Thank you very much.

    
asked by Oscar Bautista 20.12.2017 в 20:42
source

1 answer

1

Your problem is that $sumHoras = $sumHoras + $i[13]; never returns to zero when you change teachers.

Your code is quite complex to read, but somewhere, when:

if($curp_prof != $i[0])

means that you changed your teacher, and therefore at that point you should do

$sumHoras = 0;
    
answered by 20.12.2017 / 20:49
source