Ver Mensaje Individual
  #1 (permalink)  
Antiguo 13/01/2019, 12:03
dbzbr
 
Fecha de Ingreso: mayo-2015
Mensajes: 30
Antigüedad: 8 años, 11 meses
Puntos: 0
Problema al generar la media de un array

Tengo un problema al generar la media de un array ya que no me lo muestra en pantalla o no estoy haciendo algo bien.

El siguiente código muestra un formulario que al escribir '1' y enviar se imprime una lista de alumnos con sus notas, las notas se generan aleatoriamente en las funciones 'notaDificultadBaja()' y 'notaDificultadMedia()' y estas notas se guardan en el array $datos['notas'].

La última funcion llamada 'calculoNotasMedia()' la que esta con comentarios es la que tendría que generar las notas medias pero no me imprime nada por pantalla y en otros arrays si que me funciona.

Código PHP:
Ver original
  1. <?php session_start();?>
  2.  
  3. <html>
  4.     <body>
  5.         <form action="simuladorExamenes.php" method= "get">
  6.         Opción: <input type = "text" name ="opcion">
  7.         <input type = "submit">
  8.     </form>
  9.  
  10.     <?php
  11.  
  12.         if($_GET['opcion'] == 1){
  13.  
  14.             imprimeListaAlumnosNotas();
  15.         }
  16.  
  17.  
  18.         if(!existenDatosEnSesion()){
  19.  
  20.             inicializaDatos();
  21.         }
  22.  
  23.         function existenDatosEnSesion(){
  24.             return $_SESSION['datos'] != NULL;
  25.         }
  26.  
  27.         function inicializaDatos(){
  28.  
  29.             $persona1= [
  30.                 'nombre' => 'persona1',
  31.                 'notas' => []
  32.             ];
  33.  
  34.             $persona2= [
  35.                 'nombre' => 'persona2',
  36.                 'notas' => []
  37.             ];
  38.  
  39.             $persona3= [
  40.                 'nombre' => 'persona3',
  41.                 'notas' => []
  42.             ];
  43.  
  44.             $datos=[$persona1, $persona2, $persona3];
  45.             $_SESSION['datos'] = $datos;
  46.         }
  47.  
  48.         function notaDificultadBaja(){
  49.  
  50.             $datos = $_SESSION['datos'];
  51.  
  52.             foreach ($datos as $key => $value) {
  53.  
  54.                 $datos[$key]['notas'] = random_int(5 * 100 , 10 * 100) / 100;
  55.             }
  56.  
  57.             echo "<td>",$datos[$key]['notas'],"</td>";
  58.         }
  59.  
  60.         function notaDificultadMedia(){
  61.  
  62.             $datos = $_SESSION['datos'];
  63.  
  64.             foreach ($datos as $key => $value) {
  65.  
  66.                 $datos[$key]['notas'] = random_int(0 * 100, 10 * 100) / 100;
  67.             }
  68.  
  69.             echo "<td>",$datos[$key]['notas'],"</td>";
  70.         }
  71.  
  72.         function imprimeListaAlumnosNotas(){
  73.  
  74.             $datos = $_SESSION['datos'];
  75.  
  76.             echo "<div class='Encabezado'>";
  77.             echo "<h2>Lista de Alumnos y notas </h2>";
  78.             echo "</div>";
  79.  
  80.             echo "<table>";
  81.             echo "<tr>";
  82.             echo "<th>Nombre</th>";
  83.             echo "<th>E0</th>";
  84.             echo "<th>E1</th>";
  85.             echo "<th>Media</th>";
  86.             echo "</tr>";
  87.  
  88.  
  89.             foreach ( $datos as $alumno ) {
  90.  
  91.                 echo "<tr>";
  92.                 echo "<td>",$alumno['nombre']," ","</td>";
  93.  
  94.                     notaDificultadBaja();
  95.  
  96.                     notaDificultadMedia();
  97.  
  98.                     //calculoNotasMedia($alumno['notas']);
  99.  
  100.                 echo "</tr>";
  101.             }
  102.  
  103.  
  104.             echo "<tr>";
  105.             echo "<td>";
  106.             echo "<br>";
  107.             echo "<b>Notas Medias</b>";
  108.  
  109.             echo "<td/>";
  110.             echo "<tr/>";
  111.  
  112.             echo "</table>";
  113.         }
  114.  
  115.  
  116.         /*function calculoNotasMedia($pArrayNotas) {
  117.  
  118.             $sumaNotas = 0;
  119.             $numNotas = 0;
  120.                     foreach ( $pArrayNotas as $unaNota ) {
  121.                         $numNotas++;
  122.                         $sumaNotas += $unaNota;
  123.             }
  124.             return $sumaNotas/$numNotas;
  125.         }*/
  126.  
  127.     ?>
  128.  
  129.     </body>
  130. </html>

Si todo funcionase bien quería que me imprimiese esto :