0

我有一些这样的mysql表:

| IDPRAKTIKAN |       NAMAUSER | MODULKE | NILAI | STATUS_TUGASAKHIR | TAHUNAJARAN | SEMESTER |
-----------------------------------------------------------------------------------------------
|  3332081478 |   Ade Dwinanto |       0 |     3 |                 1 |       09/10 |        3 |
|  3332081478 |   Ade Dwinanto |       1 |    55 |                 1 |       09/10 |        3 |
|  3332081478 |   Ade Dwinanto |       2 |    54 |                 1 |       09/10 |        3 |
|  3332081478 |   Ade Dwinanto |       3 |    45 |                 1 |       09/10 |        3 |
|  3332081478 |   Ade Dwinanto |       4 |    62 |                 1 |       09/10 |        3 |
|  3332081478 |   Ade Dwinanto |       5 |    20 |                 1 |       09/10 |        3 |
|  3332081483 | Arya Prasettyo |       0 |     3 |                 1 |       09/10 |        3 |
|  3332081483 | Arya Prasettyo |       1 |    55 |                 1 |       09/10 |        3 |
|  3332081483 | Arya Prasettyo |       2 |    53 |                 1 |       09/10 |        3 |
|  3332081483 | Arya Prasettyo |       3 |    47 |                 1 |       09/10 |        3 |
|  3332081483 | Arya Prasettyo |       4 |    60 |                 1 |       09/10 |        3 |
|  3332081483 | Arya Prasettyo |       5 |    20 |                 1 |       09/10 |        3 |

这是我在 MySQL 上的数据(在 sqlfiddle.com 上)

这是我的 PHP 代码:

<?php
   $setting_tahunajaranget = $_GET['setting_tahunajaran'];
   $setting_kodepraktikumget = $_GET['setting_kodepraktikum'];
   $nama_praktikum = $_GET['nama_praktikum'];
   $setting_semesterget = $_GET['setting_semester'];

$query2 = "SELECT `nilai` AS semester FROM `nilaipraktikum` WHERE `modulke`= 0 LIMIT 0, 30 ";
$data   = mysql_fetch_row(mysql_query($query2));
        $query = "SELECT `idpraktikan`,namauser,`modulke`, `nilai`,`status_tugasakhir`,`tahunajaran`,`semester` FROM `nilaipraktikum` LEFT JOIN tabeluser ON tabeluser.npmuser = nilaipraktikum.idpraktikan WHERE `tahunajaran` = '$setting_tahunajaranget' AND`kodepraktikum` = '$setting_kodepraktikumget' AND `semester` = '$setting_semesterget' ";


        $result = mysql_query($query);
    if ($data = mysql_fetch_row($result)>=1)
    {

   $nilai   =   $data['nilai'];
        if ($nilai = $setting_semesterget);

echo "<table width='auto' border='1' cellpadding='3' cellspacing='3' align='center'>
  <tr>
    <td align='center' valign='middle'><strong>No</strong></td>
    <td align='center' valign='middle'><strong>Nama</strong></td>
    <td align='center' valign='middle'><strong>NPM</strong></td>";

        for($i=1;$i<=$jml_modul;$i++) {
            echo "<td align='center' valign='middle'><strong>Modul Ke-".$i."</strong></td>"; 
        }

echo    "<td align='center' valign='middle'><strong>Rata-Rata</strong></td>";

    if ($setting_status_ta=0)
    {
    }
    if ($setting_status_ta=1)
    {
    echo"<td align='center' valign='middle'><strong>Nilai Tugas Akhir</strong></td>";
    }

echo    "<td align='center' valign='middle'><strong>Nilai Akhir</strong></td>
    <td align='center' valign='middle'><strong>Grade</strong></td>
  </tr>";

        while(list($nim,$nama,$modulke,$nilai,$nilai_tugasakhir) = mysql_fetch_row($result)) {
            $array_nim[$nim] = $nim;
            $array_nama[$nim] = $nama;
            $array_nilai[$nim][$modulke]= $nilai;
            $array_nilaita[$nim][$nilai_tugasakhir]= $nilai_tugasakhir;

        }

            foreach($array_nim as $nim) {
            echo "<tr><td align='center' valign='middle'>".(++$nomor)."</td>";
            echo "<td align='center' valign='middle'>".$nim."</td>";
            echo "<td align='center' valign='middle'>".$array_nama[$nim]."</td>";

            for($i=1;$i<=$jml_modul;$i++) 
            {
                echo "<td align='center' valign='middle'>".$array_nilai[$nim][$i]."</td>"; 
                $nilaimodul += (array_sum(array($array_nilai[$nim][$i])));

            }
                            $ratarata = ($nilaimodul);

            echo "<td align='center' valign='middle'>".round ($ratarata,2)."</td>";
$ntugasakhir = $array_nilai[$nim][$jml_modul-($jml_modul-1)];


    if ($setting_status_ta=0)
    {
    }
    if ($setting_status_ta=1)
    {
            echo "<td align='center' valign='middle'>".round ($ntugasakhir,2)."</td>";
    }


$nialiakhirpraktikum = $ntugasakhir+$ratarata;
            echo "<td align='center' valign='middle'>".round ($nialiakhirpraktikum,1)."</td>";
    if ($nialiakhirpraktikum>79)
                                    $grade="A";
                                 else if ($nialiakhirpraktikum<=79 AND $nialiakhirpraktikum>67)
                                    $grade="B";
                                 else if ($nialiakhirpraktikum<=67 AND $nialiakhirpraktikum>55)
                                    $grade="C";
                                 else if ($nialiakhirpraktikum<=55 AND $nialiakhirpraktikum>44)
                                    $grade="D";
                                 else
                                    $grade="E"; 
            echo "<td align='center' valign='middle'>".$grade."</td>";
            echo "</tr>";
        }
echo "</table>";
            echo "<br/> <a href='javascript:history.back()'>Klik Disini Untuk Kembali</a>";

    }    
?>

我的问题,为什么Rata-Rata(是平均NILAI)列将数组结果与前一个数组的结果相加?有关更多详细信息,请参阅此处的图片。(链接图片:https ://www.diigo.com/item/image/3gviv/3m98?size=o )

而我想对像这张图片一样的列进行平均的结果。(链接图片:https ://www.diigo.com/item/image/3gviv/6ke4?size=o )

我的脚本有什么问题?

注意:Nilai Tugas Akhir 列 Nilai Akhir 等级尚未确定。

4

1 回答 1

0

只需添加初始化变量

$nilaimodul = 0; // try adding this to your code
for($i=1;$i<=$jml_modul;$i++) 
{
    echo "<td align='center' valign='middle'>".$array_nilai[$nim][$i]."</td>"; 
    $nilaimodul += (array_sum(array($array_nilai[$nim][$i])));
}
$ratarata = ($nilaimodul);
于 2012-12-17T11:45:07.157 回答