php - 在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);
}
解决方案
//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);
}
推荐阅读
- sqlite - 如何使用电子框架完成桌面应用程序的离线数据持久性?
- java - Spring Boot MVC 显示 404 错误
- asp.net-core-mvc - 从身份服务器 3 升级到身份服务器 4
- r - `row.names<-.data.frame`(`*tmp*`, value = value) 中的错误:不允许重复的 'row.names'
- symfony - Symfony 4文件上传不起作用
- javascript - 如何在javascript中使用正则表达式用驼峰和大写拆分字符串?
- primefaces - p:ajax 在 p:outputPanel 中不起作用
- java - 如何使用 joda DateTime 获取自己机器的本地时区
- angular - Angular 单个组件中的多个订阅
- javascript - 用于将图像保存到文件夹的离子/打字稿功能