在PHP和AJAX中重复行

时间:2018-03-22 16:59:14

标签: php jquery ajax

View Grades Page Example

正如标题所示,我遇到了PHP和AJAX的问题,让我重复行。我只想显示总数。单位和gpa,但对于每个主题,它增加了一行

AJAX:

$.ajax({
    traditional: true,
    url: "getGrades.php",
    method: "GET",
    data: {
        id: id,
        semester: semester,
        year: year
    },
    dataType: "JSON",
    success: function(data){
        $("#gradeBody").children().remove();
        $("#gradeFoot").children().remove();
        console.log(data);
        $("#gradeLoad").css("display", "block");
        for(var i = 0; i < data.length; i++){

            $("#gradeTable tbody").append('<tr class = "course"><td class = "course">'+data[i].subjectCode+'</td><td class = "course">'+data[i].subjectName+'</td>><td class = "course">'+data[i].units+'</td></td>><td class = "course">'+data[i].mg+'</td></td>><td class = "course">'+data[i].units+'</td></tr>');

            $("#gradeTable tfoot").append('<tr class = "course"><td class = "course"></td><td class = "course"></td>><td class = "course"><strong>Total No. of Units: </strong>'+data[i].un+'</td></td>><td class = "course"><strong>GPA</strong></td></td>><td class = "course">'+data[i].ave+'</td></tr>');
        }

    }

});

PHP:

[<?php
require("connect.php");
$id = $_GET\['id'\];
$year = $_GET\['year'\];
$semester = $_GET\['semester'\];
$query = "
        SELECT *, SUM(s.units) AS un, ROUND(AVG(g.fg), 2) AS ave
        FROM subjectschedule AS ss
        JOIN subject AS s 
        ON s.subjectID = ss.subjectid
        JOIN grados as g
        ON g.subjectid = s.subjectID
        WHERE g.studentid = '$id'
        AND ss.academic_year_start = '$year'
        AND ss.semester = '$semester'
        GROUP BY ss.subSchedID

        ";
$retval = mysqli_query($db, $query);
$data = array();


while($row = mysqli_fetch_assoc($retval)){
    $data\[\] = $row;
}
echo json_encode($data);

?>

我上传的图片显示当前结果,如何防止重复?

2 个答案:

答案 0 :(得分:1)

您可以通过在循环之后添加Sum和GPA来完成此操作

&#13;
&#13;
$.ajax({
  traditional: true,
  url: "getGrades.php",
  method: "GET",
  data: {
    id: id,
    semester: semester,
    year: year
  },
  dataType: "JSON",
  success: function(data) {
    $("#gradeBody").children().remove();
    $("#gradeFoot").children().remove();
    console.log(data);
    $("#gradeLoad").css("display", "block");
    for (var i = 0; i < data.length; i++) {

      $("#gradeTable tbody").append('<tr class = "course"><td class = "course">' + data[i].subjectCode + '</td><td class = "course">' + data[i].subjectName + '</td>><td class = "course">' + data[i].units + '</td></td>><td class = "course">' + data[i].mg + '</td></td>><td class = "course">' + data[i].units + '</td></tr>');


    }
    $("#gradeTable tfoot").append('<tr class = "course"><td class = "course"></ td > < td class = "course" > < /td>><td class = "course"><strong>Total No. of Units: </strong > ' + data[0].un + ' < /td></td >> < td class = "course" > < strong > GPA < /strong></td > < /td>><td class = "course">' + data[0].ave + '</td > < /tr>');


  }

});
&#13;
&#13;
&#13;

答案 1 :(得分:0)

尝试一下。我猜你需要将页脚放出for循环。

$.ajax({
            traditional: true,
            url: "getGrades.php",
            method: "GET",
            data: {
                id: id,
                semester: semester,
                year: year
            },
            dataType: "JSON",
            success: function(data){
                $("#gradeBody").children().remove();
                $("#gradeFoot").children().remove();
                console.log(data);
                $("#gradeLoad").css("display", "block");
                for(var i = 0; i < data.length; i++){
                    $("#gradeTable tbody").append('<tr class = "course"><td class = "course">'+data[i].subjectCode+'</td><td class = "course">'+data[i].subjectName+'</td>><td class = "course">'+data[i].units+'</td></td>><td class = "course">'+data[i].mg+'</td></td>><td class = "course">'+data[i].units+'</td></tr>');
                }
                    $("#gradeTable tfoot").append('<tr class = "course"><td class = "course"></td><td class = "course"></td>><td class = "course"><strong>Total No. of Units: </strong>'+data[i].un+'</td></td>><td class = "course"><strong>GPA</strong></td></td>><td class = "course">'+data[i].ave+'</td></tr>');


            }

    });