samlebo
samlebo

Reputation: 27

Displayind data in a codeigniter view using a table

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.

i want it to echo a (-) where a student does not take the subject.thanks in advance

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

the above code works but displays the subject done by students in the wrong table data field.the subjects are identified using subject codes like english is 101 while kiswahili is 102,maths is 121

Upvotes: 0

Views: 277

Answers (1)

Tom Mwenda
Tom Mwenda

Reputation: 155

I can see that the subjects are static. I would rather you have a data structure for Results, with proper relation's based on student id, course id. then you can loop all the Results and fetch the relevant subjects names and student names. eg;

<?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>";
?>

Ofcourse you will have to write Helper functions for fetching student names and calculating the mean scores.

Upvotes: 0

Related Questions