Codeigniter:从三个表生成学生成绩单

时间:2012-04-17 05:28:52

标签: mysql codeigniter codeigniter-2

我的数据库中有以下表格。表midtermfinalterm包含学生的标记。

enter image description here

现在我想在单页中显示所有学生的标记表,如下所示:

enter image description here

我知道如何在单个表中查询,但在这种情况下我遇到问题,因为有三个表。

我正在使用Codeigniter,请您帮我查看我的模型和视图文件中的代码?

提前致谢:)

这是我的控制器:

         $this->load->model('Mod_student');
     $data['records1']= $this->Mod_student->check1();
         $this->load->view('view_student',$data);

更新了部分

这是我的观点:

    <?php if(count($records1) > 0) { ?>

      <?php foreach ($records1 as $row){ ?>



   <table>
   <tr> <td><?php echo $row['StudentName']; ?></td></tr>

     <tr>

      <td><?php $midDate   =   explode(',',$row['MidDate']);
     foreach($midDate as $md)
         {
         echo $md;
         echo '<br />'  ;
         }
    ?>
         </td> 

       <td><?php $midDate   =   explode(',',$row['MidSubject']);
     foreach($midDate as $md)
         {
         echo $md;
         echo '<br />'  ;
         }
    ?>
      </td> 

       <td><?php $midDate   =   explode(',',$row['MidMarks']);
     foreach($midDate as $md)
         {
         echo $md;
         echo '<br />'  ;
         }
    ?>
       </td> 



       </tr>





     </table>




<?php  } ?>

<?php } else { echo "No Record Found";} ?>

我已经更改了我的视图文件,现在它工作正常:)但是我遇到了一些问题。如果我运行上面的脚本,我得到以下输出

enter image description here

如果您注意到我的表格midterm,您会看到id为3的学生在英语和数学方面都有相同的分数。但在我的输出中,它只显示中间一个主题的标记。它是否因为您在查询中使用distinct而发生?我尝试删除不同但后来没有显示确切的数据。

请您告诉我如何解决问题?

再次感谢您的帮助。:)

1 个答案:

答案 0 :(得分:6)

我找到了适合你的解决方案。它将为每个学生提取一条记录,包含他所有的信息。我在这里使用了mysql的GROUP_CONCAT。因此,在php端你必须爆炸该值,然后在循环中访问它。顺便说一句,这里是你可以像这样运行它的查询。

 $query= "select
          s.id   as SID,
          s.name as StudentName,
          (select group_concat(distinct midterm.date) from midterm where midterm.student_id = SID) as MidDate,
          (select group_concat(distinct midterm.Subject) from midterm where midterm.student_id = SID) as MidSubject,
          (select group_concat(midterm.marks) from midterm where midterm.student_id = SID) as MidMarks,
          (select group_concat(distinct finalterm.date) from finalterm where finalterm.student_id = SID) as FinalDate,
          (select group_concat(distinct finalterm.marks) from finalterm where finalterm.student_id = SID) as FinalMarks,
          (select distinct sum(midterm.marks) from midterm where midterm.student_id = SID) as MidTotal,
          (select distinct sum(finalterm.marks) from finalterm where finalterm.student_id = SID) as FinalTotal
          from students as s
          left join midterm as m on m.student_id = s.id 
          left join finalterm as f on f.student_id = s.id
          and f.subject = m.subject
          group by s.name;";

    $query  =   $this->db->query($query);

OR Active Record Version

$data   =   array(
                    's.id   as SID',
                    's.name as StudentName',
                    '(select group_concat(distinct midterm.date) from midterm where midterm.student_id = SID) as MidDate',
                    '(select group_concat(distinct midterm.Subject) from midterm where midterm.student_id = SID) as MidSubject',
                    '(select group_concat(midterm.marks) from midterm where midterm.student_id = SID) as MidMarks',
                    '(select group_concat(distinct finalterm.date) from finalterm where finalterm.student_id = SID) as FinalDate',
                    '(select group_concat(distinct finalterm.marks) from finalterm where finalterm.student_id = SID) as FinalMarks',
                    '(select distinct sum(midterm.marks) from midterm where midterm.student_id = SID) as MidTotal',
                    '(select distinct sum(finalterm.marks)from finalterm where finalterm.student_id = SID) as FinalTotal'               
                    );
    $this->db->select($data);
    $this->db->group_by('s.name');
    $this->db->group_by('s.name');
    $this->db->from('students as s');
    $this->db->join('midterm as m','m.student_id = s.id','left');
    $this->db->join('finalterm as f','f.student_id = s.id','left');
    $this->db->group_by('s.name');

使用distinct关键字。以下是使用此功能的一些说明。 1.应该爆发每个组concat以在php端使用。喜欢这个

<?php echo $row['StudentName']?>

和群组Concat项目表格查询

 $midDate   =   explode(',',$row['MidDate']);
 foreach($midDate as $md)
 {
 echo $md;
 echo '<br />'  ;
 }