php - Displayind data in a codeigniter view using a table -
hi having problem in sorting correct <td> display subject results students. have 11 subjects offered in school , senior students take 8 subjects because 4 electives @ level. when fetching results senior students , displaying when student not take subject still echoes result in wrong field.my code below not distinguish e.g if <td> physics or biology.
i want echo (-) student not take 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 display names of students $i = 1; foreach ($overallresults $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 $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 $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 displays subject done students in wrong table data field.the subjects identified using subject codes english 101 while kiswahili 102,maths 121
i can see subjects static. rather have data structure results, proper relation's based on student id, course id. can loop results , fetch relevant subjects names , 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 $result){ echo "<tr>"; echo "<td>".$count."</td>"; echo "<td>".$students[$result['studentid']]."</td>"; foreach($subjects $key => $value){ $marks = $result['score'][$key]!=null?$result['score'][$key]:'-'; echo "<td>".$marks."</td>"; } echo "</tr>"; $count++; } echo "</table>"; ?>
ofcourse have write helper functions fetching student names , calculating mean scores.
Comments
Post a Comment