I am trying to save the output that this code shows me on the screen in a .txt keeping the format "name, average grade, approved, failed". Line example in the .txt: Ana,7,5,0 (only the values, the table no.)
<?php
$notas = array(
'Miguel' => array('Matematicas'=>5, 'Tecnologia'=>5, 'Historia'=>6, 'Ingles'=>8),
'Ana' => array('Matematicas'=>7, 'Ingles'=>8, 'Lengua'=>6, 'Fisica'=>9, 'Historia'=>5),
'Luis' => array('Matematicas'=>4, 'Tecnologia'=>4, 'Lengua'=>5, 'Fisica'=>4, 'Historia'=>7),
'Maria' => array('Matematicas'=>6, 'Tecnologia'=>5, 'Historia'=>6, 'Ingles'=>4),
'Antonio' => array('Matematicas'=>4, 'Tecnologia'=>5, 'Lengua'=>5, 'Fisica'=>4, 'Historia'=>4, 'Ingles'=>6),
'Raul' => array('Matematicas'=>4, 'Tecnologia'=>5, 'Lengua'=>7, 'Fisica'=>5, 'Historia'=>6, 'Ingles'=>8)
);
$arrayFinal=array();
ksort($notas);
echo "<table border='1'><tr><th>Nombre del alumno</th><th>Nota Media</th><th>Aprobados</th><th>Suspensos</th></tr>";
foreach($notas as $nombre=>$array)
{
$aprobados=0;
$suspensos=0;
$acumuladorNotas=0;
$contadorNotas=0;
$notaMedia=0;
foreach($array as $asig=>$nota)
{
$contadorNotas++;
$acumuladorNotas+=$nota;
$notaMedia=$acumuladorNotas/$contadorNotas;
if($nota>=5)
{
$aprobados++;
}
else
{
$suspensos++;
}
$arrayFinal[$nombre]['Media'] = $notaMedia;
$arrayFinal[$nombre]['aprobados'] = $aprobados;
$arrayFinal[$nombre]['suspensos'] = $suspensos;
}
echo "<tr><td>".$nombre."</td><td>".number_format($notaMedia,2)."</td><td>".$aprobados."</td><td>".$suspensos."</td></tr>";
}
echo "</table>";
//print_r ($arrayFinal);
ksort($arrayFinal);
$ficheroSalida=fopen("salida.txt","a");
foreach($arrayFinal as $nombres=>$array)
{
foreach ($array as $indice => $valor)
{
fwrite($ficheroSalida,$nombres.",".$valor.PHP_EOL);
}
}
fclose($ficheroSalida);
?>
I have tried several things like putting: $arrayFinal["approved"]=$approved; but it only saves it for the first student and in the .txt it doesn't save it, keeping the format. I hope you can help me, thank you.
Screen output:
.txt:
Update: Now it saves all the data fine, but when it creates the .txt file it repeats the values (student name) and puts them one below the other:
You can simplify your code a bit as follows:
In your code, for each grade you set the array $arrayFinal (Average, approved, failed), you would have to do this when you finish processing the grades of each student (that is, outside the most nested foreach, where you echo the row for table).
Also directly there you could write the line to the file (instead of saving the data in the $arrayFinal array), and not use two foreach afterwards to do it.
After analyzing the comments on your post, I suspect that the problem is in the following assignment
$arrayFinal[$nombre]=$notaMedia;
, since you are only assigning$notaMedia
each user's assignment, when what (I think) you want to do is store all their values. Thus, it is as easy as adding themkeys
to the same array:and then iterate to print it all to the TXT file. I recommend you uncomment the line
//print_r ($arrayFinal);
so that you can see the contents of the array on the screen beforehand and thus be able to self-debug the logic errors in your code.You may find it helpful to use more concrete variable names than
Maybe that way you would see how within you you
$arrayFinal
are saving the$nombre
student's and his$notaMedia
and not the rest of the values that you also want.UPDATE :
Modify your code so you can see more clearly where the error is. Instead of writing directly to the file with
fwrite
try creating a type variable$linea
that contains the line of text you want to write to the file. Useecho
orprint_r
in your code for each iteration of your loops if necessary.The code you are looking for would look something like this: