【发布时间】:2019-07-23 03:00:29
【问题描述】:
我想为我创建的每个活动计算我的表中所有在 studentattendees 表中具有相同 eventId 的结果。这是我的数据库中两个表的 sn-p。活动和学生参加者表。
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);
}
【问题讨论】:
-
试试这个
$this->db->get()->result()->num_row()而不是$count = $this->db->count_all_results(); -
@DanishAli 请检查下面的答案,看看需要改变什么......谢谢
标签: php mysql database codeigniter phpmyadmin