【问题标题】:counting the results in codeigniter在codeigniter中计算结果
【发布时间】: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-&gt;db-&gt;get()-&gt;result()-&gt;num_row() 而不是$count = $this-&gt;db-&gt;count_all_results();
  • @DanishAli 请检查下面的答案,看看需要改变什么......谢谢

标签: php mysql database codeigniter phpmyadmin


【解决方案1】:
//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);
}

【讨论】:

  • 请检查我的 cmets,这也是我的担忧。我不确定我是否在我的 foreach 中传递了正确的变量。
  • echo $count = $this->db->count_all_results();
  • echo $count 结果是数组或单个变量。
猜你喜欢
  • 2011-04-09
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多