在codeigniter中计算结果

时间:2019-03-01 05:35:25

标签: php mysql database codeigniter phpmyadmin

我想为我创建的每个活动计算学生表中具有相同eventId的表中的所有结果。这是我数据库中两个表的摘要。活动和学生参加者表。

studentattendees表:

studentattendeestable

activity表:

activitytable

我想知道如何制作一个模型函数来对所有结果进行计数,其中学生参加者中的eventId等于活动表中的activityId。另外,我将如何回应这一观点。这是我在模型,视图,控制器中的代码示例。

//view
public function getReportAttendees(){

        $this->db->order_by('activity.activityId', 'DESC');
        $query = $this->db->get('activity');

        // date status
        $resulting = $query->result_array();
        foreach($resulting as $resultings){
        $activity = $resultings['activityId'];

        $this->db->join('activity', 'activity.activityId = 
studentattendees.eventId');
        $this->db->where('eventId',$activity);
        $this->db->from('studentattendees');
        $count = $this->db->count_all_results();
        foreach($count as $counts){
                array_push($countall, $count);
            }
    }

    return $countall;

}

//view

<?php 
  if($attendee){
    foreach($attendee as $attendees){
?>
    <td><?php echo $attendees; ?></td>
    <?php
    }
  }
?>

//controller 
public function ReportGenerated(){
    $data['attendee'] = $this->u->getReportAttendees();
    $this->load->view('logmanagement/reportgenerated', $data);
}

1 个答案:

答案 0 :(得分:0)

//modal
public function getReportAttendees(){
    $countall = array();
    $this->db->order_by('activity.activityId', 'DESC');
    $query = $this->db->get('activity');
    $resulting = $query->result_array();
    foreach($resulting as $resultings){
        $activity = $resultings['activityId'];
        $this->db->join('activity', 'activity.activityId = studentattendees.eventId');
        $this->db->where('eventId',$activity);
        $this->db->from('studentattendees');
        $count = $this->db->count_all_results();
        foreach($count as $counts){
            array_push($countall, $count);
        }
    }
    return $countall;

}

//view

<?php 
  if($attendee){
    foreach($attendee as $attendees){
?>
    <td><?php echo $attendees; ?></td>
    <?php
    }
  }
?>

//controller 
public function ReportGenerated(){
    $data['attendee'] = $this->u->getReportAttendees();
    $this->load->view('logmanagement/reportgenerated', $data);
}