如何合并学生分阶段成绩数据,实现同一科目成绩同行展示?
问题描述
需要展示学生的**Prelim(期初)、Midterm(期中)、Finals(期末)**成绩,当前问题是同一科目的不同阶段成绩被拆分成多行显示,目标是让同一科目各阶段成绩在同一行展示。
现有代码
View 代码
<?php foreach($grades as $grade): ?> <tr> <th scope="row"><?php echo $grade['subCode']; ?></th> <td><?php echo $grade['subject']; ?></td> <?php if($grade['gradePeriod']=="Prelim"): ?> <td><?php echo $grade['grade']; ?></td> <?php else: ?> <td>0</td> <?php endif; ?> <?php if($grade['gradePeriod']=="Midterm"): ?> <td><?php echo $grade['grade']; ?></td> <?php else: ?> <td>0</td> <?php endif; ?> <?php if($grade['gradePeriod']=="Finals"): ?> <td><?php echo $grade['grade']; ?></td> <?php else: ?> <td>0</td> <?php endif; ?> <td><?php echo $grade['remarks']; ?></td> </tr> <?php endforeach; ?>
Model 代码
public function gradePeriod(){ $this->db->select('subCode,subject,grade,gradePeriod,remarks'); $this->db->from('tbl_college_grades'); $this->db->where('studentID', ' 200171419'); $this->db->where('schoolYear','2022-2023'); $this->db->where('semester','First Semester'); $query = $this->db->get(); return $query->result_array(); }
问题原因
当前数据库查询返回的每条记录对应单个科目+单个阶段的成绩,foreach循环时每个阶段都会生成一行,导致同一科目被拆分成多行显示。
解决方案
推荐在Model层重组数据(符合MVC架构,数据逻辑与视图分离),也可以在View层临时处理。
方案1:Model层重组数据
修改Model方法,将同一科目的不同阶段成绩合并到同一数组中:
public function gradePeriod(){ $query = $this->db->select('subCode,subject,grade,gradePeriod,remarks') ->from('tbl_college_grades') ->where('studentID', '200171419') ->where('schoolYear','2022-2023') ->where('semester','First Semester') ->get(); $consolidatedGrades = []; foreach($query->result_array() as $row){ $subCode = $row['subCode']; // 初始化科目结构(首次遇到该科目时) if(!isset($consolidatedGrades[$subCode])){ $consolidatedGrades[$subCode] = [ 'subCode' => $row['subCode'], 'subject' => $row['subject'], 'prelim' => 0, 'midterm' => 0, 'finals' => 0, 'remarks' => $row['remarks'] ]; } // 根据成绩阶段赋值 switch(strtolower($row['gradePeriod'])){ case 'prelim': $consolidatedGrades[$subCode]['prelim'] = $row['grade']; break; case 'midterm': $consolidatedGrades[$subCode]['midterm'] = $row['grade']; break; case 'finals': $consolidatedGrades[$subCode]['finals'] = $row['grade']; break; } } // 转换为索引数组,方便View遍历 return array_values($consolidatedGrades); }
对应的View代码简化为:
<?php foreach($grades as $grade): ?> <tr> <th scope="row"><?php echo $grade['subCode']; ?></th> <td><?php echo $grade['subject']; ?></td> <td><?php echo $grade['prelim']; ?></td> <td><?php echo $grade['midterm']; ?></td> <td><?php echo $grade['finals']; ?></td> <td><?php echo $grade['remarks']; ?></td> </tr> <?php endforeach; ?>
方案2:View层临时处理(不推荐,仅作备选)
如果无法修改Model,可在View层临时合并同一科目的数据:
<?php $currentSub = ''; $currentGrade = [ 'subCode' => '', 'subject' => '', 'prelim' => 0, 'midterm' => 0, 'finals' => 0, 'remarks' => '' ]; foreach($grades as $grade): if($grade['subCode'] !== $currentSub){ // 输出上一个科目的行(首次循环跳过) if($currentSub !== ''): ?> <tr> <th scope="row"><?php echo $currentGrade['subCode']; ?></th> <td><?php echo $currentGrade['subject']; ?></td> <td><?php echo $currentGrade['prelim']; ?></td> <td><?php echo $currentGrade['midterm']; ?></td> <td><?php echo $currentGrade['finals']; ?></td> <td><?php echo $currentGrade['remarks']; ?></td> </tr> <?php endif; // 初始化当前科目数据 $currentSub = $grade['subCode']; $currentGrade = [ 'subCode' => $grade['subCode'], 'subject' => $grade['subject'], 'prelim' => 0, 'midterm' => 0, 'finals' => 0, 'remarks' => $grade['remarks'] ]; } // 赋值对应阶段成绩 match($grade['gradePeriod']){ 'Prelim' => $currentGrade['prelim'] = $grade['grade'], 'Midterm' => $currentGrade['midterm'] = $grade['grade'], 'Finals' => $currentGrade['finals'] = $grade['grade'], }; endforeach; // 输出最后一个科目 if($currentSub !== ''): ?> <tr> <th scope="row"><?php echo $currentGrade['subCode']; ?></th> <td><?php echo $currentGrade['subject']; ?></td> <td><?php echo $currentGrade['prelim']; ?></td> <td><?php echo $currentGrade['midterm']; ?></td> <td><?php echo $currentGrade['finals']; ?></td> <td><?php echo $currentGrade['remarks']; ?></td> </tr> <?php endif; ?>
内容的提问来源于stack exchange,提问作者Coder Codes
相关产品推荐
相关产品推荐

