我试图在codeigniter中实现以下结果
SELECT location, COUNT(location), AVG(review) FROM progrodb.tickets WHERE datesubmitted BETWEEN '2018-9-1' AND '2018-9-30' AND location = 'location'
输出需要:;
Location|Total Tickets|Avg Review<br>
location|3 |4.5
此表应包括每个位置的结果。sql语句按原样提供单个位置的结果现在我需要对总共22个位置执行此操作。
我尝试了以下尝试,但是在var\u dump()结果返回null之后
public function generatereport(){
// Set Page Title
$this->data['page_title'] = 'Generate Report';
$rules = $this->support_m->rules_report;
$this->form_validation->set_rules($rules);
$startdate = $this->input->post('startdate');
$enddate = $this->input->post('enddate');
define('locations', array('Shoppers Fair Blue Diamond', 'Shoppers Fair Burke Road', 'Shoppers Fair Brunswick', 'Shoppers Fair Duhaney Park', 'Shoppers Fair Greater Portmore', 'Shoppers Fair View', 'Shoppers Fair Junction', 'Shoppers Fair Liguanea', 'Shoppers Fair Manchester'));
if ($this->form_validation->run() == TRUE){
$results = $this->db->select('location, count(location) as location_count, AVG(review) as review_avg')
->where('datesubmitted BETWEEN "'.$startdate.'" AND "'.$enddate.'"')
->group_by('location')
->get('tickets')->result();
var_dump($results);
}
// Load view
$this->data['subview'] = 'admin/tickets/report';
$this->load->view( 'admin/body', $this->data );
}
现在我正在尝试将结果传递给视图,但是我收到了errors undefined变量:reports并尝试获取non object的属性。
5条答案
按热度按时间vuktfyat1#
以下代码按预期工作:
在控制器中
在视图中
感谢所有的贡献者。
c7rzv4ha2#
如果我知道你想要什么,像这样的东西可以帮助你:
为了便于阅读,我把计数和平均值取了个别名
t5fffqht3#
我想你需要这样的短信
tzxcd3kk4#
我想你需要having子句来表示聚合函数。像这样的方法可能有用:
uinbv5nw5#
您可以使用以下方法为多个位置生成报告: