1
I need to calculate the final grade average of a class, I tried using the function avg
SQL, but I was not successful, the code is this:
<?php
$idTurma = $_GET["idTurma"];
$idDisciplina = $_GET["idDisciplina"];
include ("conectar.php");
$result = mysql_query("SELECT a.nome, n.faltas1, n.faltas2, n.sem1, n.sem2, a.idaluno, d.iddisciplina from aluno a, nota n, turma t, disciplina d where t.idturma=$idTurma and a.idturma=t.idturma and n.iddisciplina=$idDisciplina and d.iddisciplina=n.iddisciplina and n.idaluno=a.idaluno");
$linha = mysql_num_rows($result);
if ($linha) {
echo "<table border=1>";
echo "<tr align=center>";
echo "<td rowspan=2>Alunos</td>";
echo "<td colspan=2>1º SEM</td>";
echo "<td colspan=2>2º SEM</td>";
echo "<td rowspan=2>Média Final</td>";
echo "<td rowspan=2>Editar</td>";
echo "</tr>";
echo "<tr align=center>";
echo "<td>Nota</td>";
echo "<td>Faltas</td>";
echo "<td>Nota</td>";
echo "<td>Faltas</td>";
echo "</tr>";
while ($dados = mysql_fetch_array($result)){
$nome = $dados['nome'];
$sem1 = $dados['sem1'];
$faltas1 = $dados['faltas1'];
$sem2 = $dados['sem2'];
$faltas2 = $dados['faltas2'];
$idDisciplina = $dados ['iddisciplina'];
$idAluno = $dados ['idaluno'];
echo "<tr align=center>";
echo "<td>".$nome."</td>";
echo "<td>".$sem1."</td>";
echo "<td>".$faltas1."</td>";
echo "<td>".$sem2."</td>";
echo "<td>".$faltas2."</td>";
echo "<td>".$mediafinal."</td>";
echo "<td><a href='editarnota.php?idAluno=$idAluno&idDisciplina=$idDisciplina'><img src='images/editar.gif'></td>";
echo "</tr>";
}
echo "</table>";
}
?>
You could post the code with the "avg" function you tried?
– Thomas
select avg(sem1), (sem2) from note Where idaluno=2(example), in the note table also has fault field, so do not know how to take two values and make the average, the final average is not saved in the database, only shown in the table with the php result
– Maria Joana
I need help in both sql code and php to get the variables and show in the table :(
– Maria Joana
How is the structure of your table
nota
?– Thomas
idaluno, iddisciplina, sem1, sem1, faltas1 e faltas2
– Maria Joana
Since you have 2 notes in a row, the AVG function will not bring the expected result, as it averages a field based on all records returned from the query.
– Thomas
To average, it would be something like
SELECT ((sem1+sem2)/2) AS media FROM nota WHERE idaluno = 2
– Thomas
thanks, and how I would store this result in a variable and then show in the table?
– Maria Joana
This question seems to be decontextualized because it did not demonstrate the author’s effort in solving the problem.
– Bruno Augusto