How to jump from mysql records using a php loop, and from each iterated record, calculate between field values?

0

What this code does is to receive in a variable the number of classes taught, in array the number of absences for each student, in another'array' the idGroup, which is always the same, and in another array receives the idAlumno, these data are sent from a table as a form. What I was trying to do is that based on num. of sessions and assistances by means of a rule of 3 to calculate the percentage of attendance obtained from each student. This part seems to work well, but apart you have to calculate the average of 3 grades of each student, these grades are retrieved from the database, you must evaluate if the percentage of attendance of each student is greater than or equal to 80, save in the corresponding record the average obtained previously, of being less than 80 must have a failing grade = 5.

What he managed to do well is the jump from registration to registration, to make those calculations in each one of them. I was trying to do it by initializing a variable with the first idAlumno found and then compare it in the loop if it is the same as that recovered by another variable. It's more of a logic error.

//CALCULO DE PROMEDIOS DE ALUMNO Y % DE ASISTENCIAS
 
  elseif( (isset($_POST['numsesiones']) ) && (!empty($_POST['asistotales'])) ) {
    $grupo4      = $_POST['idgpo']; //arreglo idgrupo[] siempre es el mismo
    $gpo4 = $grupo4[0]; //el idGrupo es el mismo en todo el arreglo
    $alumno4     = $_POST['idalumno']; //arreglo idalumno[]

     $consulta1 = "SELECT A.matricula, A.nombre, AG.parcial1, AG.parcial2, AG.parcial3, AG.final, AG.idAlumno, AG.idGrupo FROM alumno A, alumno_grupo AG, materias M, periodos Pr, profesores P, grupos G WHERE M.idMateria = G.materia and A.idAlumno = AG.idAlumno and G.idGrupo = AG.idGrupo and G.profesor = P.idProfesor and G.periodo = Pr.idPeriodo and AG.idGrupo = $gpo4 ORDER BY A.nombre";

     $res = mysqli_query($conexion,$consulta1);
     $res2 = mysqli_query($conexion,$consulta1);

     $fila = $res->fetch_assoc(); //array asociativo para obtener el primer registro de alumno
     $fila2 = $res2->fetch_assoc(); //array asociativo para obtener los demas registros
  
     $nalumno = count($alumno4); //cuenta los elementos del arreglo de los id alumno[]

      $asistenciasalumno  = $_POST['asistotales']; //valores de arreglo recibido
      $numsesiones  = $_POST['numsesiones'];  //1 valor recibido de un input
      $bandera = $fila['idAlumno'];  //asignamos a variable el primer registro de alumno encontrado

       for ($a = 0; $a < $nalumno; $a++) {  //for recorre elementos del array[] idAlumno

            $alum = $alumno4[$a]; //variable toma valor del arreglo en el id
            $gpos = $grupo4[$a]; //variable toma el valor del arreglo de idgrupo, (siempre es el mismo)

            //asignanle a variable el indice del arreglo
            $asis = $asistenciasalumno[$a];

            $regla = $asis * 100;
            $asis = $regla/$numsesiones;  //regla de 3 para sacr porcentaje de asistencia, 
            $asis = round($asis);
            echo $asis; //prueba
            echo "<br>"; //prueba

            $sqlp = mysqli_query($conexion, "UPDATE alumno_grupo SET asistencias = '{$asis}'
                  WHERE idAlumno = '{$alum}' AND idGrupo = '{$gpos}'") or die (mysqli_error($conexion));

          while ($fila2 = $res2->fetch_assoc()) {

            if($bandera == $fila2['idAlumno']) { //si la bandera es igual al primer idAlumno
            //obteniendo las calificaciones de los 3 parciales por alumno
            $p1b = $fila2['parcial1'];
            $p2b = $fila2['parcial2'];
            $p3b = $fila2['parcial3'];
        
            $suma = $p1b+$p2b+$p3b;
            $promedio = $suma/3;  
            } else {
          
            $p1b = $fila2['parcial1'];
            $p2b = $fila2['parcial2'];
            $p3b = $fila2['parcial3'];
        
            $suma = $p1b+$p2b+$p3b;
            $promedio = $suma/3;  
            }
            $bandera = $fila['idAlumno'];
          }
             if ($asis >= 80) {           //DE SER MENOR EL % DE ASSITENCIAS OBTENIDO DEL 80
                  $sqlb = mysqli_query($conexion, "UPDATE alumno_grupo SET final = '{$promedio}'
                  WHERE idAlumno = '{$alum}' AND idGrupo = '{$gpos}'") or die (mysqli_error($conexion));
             } elseif ($asis < 80) {  
                  $sqlb = mysqli_query($conexion, "UPDATE alumno_grupo SET final = 5
                  WHERE idAlumno = '{$alum}' AND idGrupo = '{$gpos}'") or die (mysqli_error($conexion));
             } 
             } //for recorre                          
      } //if empty numsesiones && isset asistotales/
    
asked by Armando Arellano 21.12.2016 в 01:59
source

1 answer

1

Simplify your code a bit to make compression easier. One of the problems you had was that you were going through an array of incremetal keys using a FOR incorrectly I think it was because of this that you complicated the logic by keeping a flag with the id of the student you were traveling.

I'll leave the code fixed and below I'll explain.

<?php

if(isset($_POST['numsesiones']) && !empty($_POST['asistotales'])) {
    $id_grupo = $_POST['idgpo'][0]; // Valor fijo, es siempre igual
    $alumnos = $_POST['idalumno']; // Array de idalumno[]
    $asistencias  = $_POST['asistotales']; //valores de arreglo recibido
    $numsesiones  = $_POST['numsesiones'];  //1 valor recibido de un input
    $sql = "SELECT A.matricula, A.nombre, AG.parcial1, AG.parcial2, AG.parcial3, AG.final, AG.idAlumno, AG.idGrupo FROM alumno A, alumno_grupo AG, materias M, periodos Pr, profesores P, grupos G WHERE M.idMateria = G.materia and A.idAlumno = AG.idAlumno and G.idGrupo = AG.idGrupo and G.profesor = P.idProfesor and G.periodo = Pr.idPeriodo and AG.idGrupo = $gpo4 ORDER BY A.nombre";
    $query = mysqli_query($conexion,$sql);

    for ($a = 0; $a < count($alumnos); $a++) {  //for recorre elementos del array[] idAlumno

        $id_alumno = $alumnos[$a];

        //asignanle a variable el indice del arreglo
        $alumno_asistencias = $asistencias[$a];

        //Promedio de asistencias
        $alumno_asistencias_promedio = round(($alumno_asistencias * 100) / $numsesiones);

        echo $alumno_asistencias_promedio; //prueba
        echo "<br>"; //prueba

        //Update
        mysqli_query($conexion, "UPDATE alumno_grupo SET asistencias = '{$alumno_asistencias_promedio}' WHERE idAlumno = '{$id_alumno}' AND idGrupo = '{$id_grupo}'") or die (mysqli_error($conexion));

        while ($row = $query->fetch_assoc()) {

            $promedio = ($row['parcial1'] + $row['parcial2'] + $row['parcial3'])/3;

            if ($alumno_asistencias_promedio >= 80){
                $sqlb = mysqli_query($conexion, "UPDATE alumno_grupo SET final = '{$alumno_asistencias_promedio}' WHERE idAlumno = '{$id_alumno}' AND idGrupo = '{$id_grupo}'") or die (mysqli_error($conexion));
            }else{
                $sqlb = mysqli_query($conexion, "UPDATE alumno_grupo SET final = 5 WHERE idAlumno = '{$id_alumno}' AND idGrupo = '{$id_grupo}'") or die (mysqli_error($conexion));
            }
        }
    }
}

?>

The truth only correji the code using common sense but I do not finish to understand the values that you are receiving, in case it does not work you could add to your question a print_r / var_dump of everything you are receiving by POST.

    
answered by 21.12.2016 / 12:57
source