Calculation of the average in php

Asked

Viewed 2,653 times

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?

  • 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

  • I need help in both sql code and php to get the variables and show in the table :(

  • How is the structure of your table nota?

  • idaluno, iddisciplina, sem1, sem1, faltas1 e faltas2

  • 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.

  • To average, it would be something like SELECT ((sem1+sem2)/2) AS media FROM nota WHERE idaluno = 2

  • thanks, and how I would store this result in a variable and then show in the table?

  • 5

    This question seems to be decontextualized because it did not demonstrate the author’s effort in solving the problem.

Show 4 more comments

1 answer

1


As you have 2 notes in a row, the function AVG will not bring the expected result, because it averages a field based on all records returned from the query.
To take the average, you will have to add the contents of the 2 fields and divide by 2.

<?php
$idTurma = $_GET["idTurma"];
$idDisciplina = $_GET["idDisciplina"];

include ("conectar.php");

$result = mysql_query("SELECT
    a.nome,
    n.faltas1,
    n.faltas2,
    ((n.sem1 + n.sem2) / 2) AS media_final,
    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>
         <tr align=center>
         <td rowspan=2>Alunos</td>
         <td colspan=2>1º SEM</td>
         <td colspan=2>2º SEM</td>
         <td rowspan=2>Média Final</td>
         <td rowspan=2>Editar</td>
         </tr>
         <tr align=center>
         <td>Nota</td>
         <td>Faltas</td>
         <td>Nota</td>
         <td>Faltas</td>
         </tr>";

    while ($dados = mysql_fetch_array($result, MYSQL_ASSOC)){ //use a constante MYSQL_ASSOC para poder usar a array com os aliases da query
        $nome = $dados['nome'];
        $sem1 = $dados['sem1'];
        $faltas1 = $dados['faltas1'];
        $sem2 = $dados['sem2'];
        $faltas2 = $dados['faltas2'];
        //agora para pegar a média, é só acessar o alias media_final, que temos na query e que está presente em $dados
        $mediafinal = $dados['media_final'];

        $idDisciplina = $dados  ['iddisciplina'];
        $idAluno = $dados ['idaluno'];
        echo "<tr align=center>
        <td>{$nome}</td>
        <td>{$sem1}</td>
        <td>{$faltas1}</td>
        <td>{$sem2}</td>
        <td>{$faltas2}</td>
        <td>{$mediafinal}</td>
        <td><a href='editarnota.php?idAluno={$idAluno}&idDisciplina={$idDisciplina}'><img src='images/editar.gif'></td>
        </tr>";
    }
    echo "</table>";
}
?>
  • sensational, thank you so much!!!!

  • If it worked, don’t forget to flag the answer as correct. If it helped you, it will help others with the same question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.