首页 > 解决方案 > 在codeigniter中计算结果

问题描述

对于我创建的每个活动,我想计算我的表中所有在 studentattendees 表中具有相同 eventId 的结果。这是我的数据库中两个表的片段。活动和学生参加者表。

studentattendees桌子:

学生参加者稳定

activity桌子:

活动表

我想知道如何制作一个模型函数来计算我的 studentattendees 中的 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);
}

标签: phpmysqldatabasecodeigniterphpmyadmin

解决方案


//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);
}

推荐阅读