0
Hi am having problem in sorting the correct <td> to display subject results for students. i have 11 subjects offered in the school and senior students take 8 subjects because 4 are electives at this level. so when fetching results for the senior students and displaying when the student does not take that subject it still echoes the result in the wrong field.my code below does not distinguish e.g if the <td> is physics or biology.

我希望它在学生不参加该主题的情况下呼应(-)。在此先感谢

<table border='1'>
        <tr><th>Position</th><th>Students</th><th>English</th><th>Kiswahili</th><th>Maths</th><th>Biology</th><th>Physics</th><th>Chemistry</th><th>History</th><th>Geography</th><th>CRE</th><th>Agriculture</th><th>Business</th><th>Total Marks</th><th>Mean Grade</th><th>Aggregate Points</th><th>Action</th>
        </tr> 
        <?php
//loop to display the names of students
        $i = 1;
        foreach ($overallresults as $result) {
            echo "<tr>";
            echo "<td>";                
        echo $i++;
        echo "</td>";
        $admNo = $result->admNo;
        $total_marks = $result->total_marks;
        $agp = $result->aggregate_points;
        $mean_grade = $result->mean_grade;
        $agp = $result->aggregate_points;
        $result_id = $result->result_id;
        $fname = "";

        foreach ($students as $student) {
            // print_r($student);
            $admNo1 = $student->admNo;
            if ($admNo == $admNo1) {
                // print_r($student);
                $fname = $student->firstName;
                $mname = $student->middleName;
                $lname = $student->lastName;
                //}
                // }
                //echo "<tr>";

                echo "<td>";
                echo $fname . " " . $mname . " " . $lname;
                echo "</td>";
            }
        }   

        foreach ($subjectresults as $subresult) {
            // print_r($result); 
            $score = "0";
            $admNo3 = $subresult->admNo;
            $subCode = $subresult->subCode;
            $score = $subresult->score;

            if ($admNo == $admNo3) {
                if ($subCode == '232') {
                    $score = $score;
                }
                if ($subCode == '101') {
                    echo "<td>";
                    echo $score;
                    echo "</td>";
                }


                if ($subCode == '102') {
                    echo "<td>";
                    echo $score;
                    echo "</td>";
                }
                if ($subCode == '121') {
                    echo "<td>";
                    echo $score;
                    echo "</td>";
                }
                if ($subCode == '231') {
                    echo "<td>";
                    echo $score;
                    echo "</td>";
                }
                if ($subCode == '232') {
                    echo "<td>";
                    echo $score;
                    echo "</td>";
                }

                if ($subCode == '233') {
                    echo "<td>";
                    echo $score;
                    echo "</td>";
                }
                if ($subCode == '311') {
                    echo "<td>";
                    echo $score;
                    echo "</td>";
                }
                if ($subCode == '312') {
                    echo "<td>";
                    echo $score;
                    echo "</td>";
                }
                if ($subCode == '313') {
                    echo "<td>";
                    echo $score;
                    echo "</td>";
                }
                if ($subCode == '443') {
                    echo "<td>";
                    if (!$score) {
                        echo 0;
                    } else {
                        echo $score;
                    }
                    echo "</td>";
                }
                if ($subCode == '565') {
                    echo "<td>";
                    echo $score;
                    echo "</td>";
                }
            }
        }
            ?>


        <?php
        if (isset($term)) {
            $term = $term;
        }
        if (isset($form)) {
            $form = $form;
        }

        if (isset($year)) {
            $year = $year;
        }
        if (isset($examCategory)) {
            $examCategory = $examCategory;
        }

        //if ($admNo == $admNo1) {
        // print_r($student);
        //}
        // }

        echo "<td>";
        echo $total_marks;
        echo "</td>";
        echo "<td>";
        echo $mean_grade;
        echo "</td>";
        echo "<td>";
        echo $agp;
        echo "</td>";
        echo "<td>";

        echo "</td>";

        //}
    }
    ?>

    </table>
    <?php
}
?>
</div>

上面的代码有效,但在错误的表格数据字段中显示学生完成的科目。科目是使用科目代码识别的,例如英语是 101,斯瓦希里语是 102,数学是 121

4

1 回答 1

0

我可以看到主题是静态的。我希望你有一个结果的数据结构,具有基于学生 ID、课程 ID 的适当关系。然后您可以循环所有结果并获取相关主题名称和学生姓名。例如;

<?php
$results = array();
$subjects = array(101=>"English", 102=>"Kiswahili",103=>"Physics",104=>"Chemistry");
$students = array("tom","dick","ally");
$result1 = array(
'studentId'=>1,
'score'=>array(
      101=>20,
      102=>30,
      103=>30,
      104=>45
),);

$result2 = array(
'studentId'=>2,
'score'=>array(
      101=>34,
      102=>54,
      103=>77
),);
$results[] = $result1;
$results[] = $result2;
echo "<table border='1'>";
echo "<tr>";
echo "<th>#</th><th>Student</th>";
for($i = 101; $i < 105; $i++){
echo "<th>".$subjects[$i]."</th>";
}
echo "</tr>";
$count = 1;
foreach($results as $result){

echo "<tr>";
echo "<td>".$count."</td>";
echo "<td>".$students[$result['studentId']]."</td>";
foreach($subjects as $key => $value){
$marks = $result['score'][$key]!=null?$result['score'][$key]:'-';
echo "<td>".$marks."</td>";
}
echo "</tr>";
$count++;
}
echo "</table>";
?>

当然,您必须编写帮助函数来获取学生姓名和计算平均分数。

于 2013-05-10T08:03:41.333 回答