【发布时间】:2018-05-24 17:15:51
【问题描述】:
我正在尝试将数据从我的数据库中提取到具有表标题“学生总数 | 男性 | 女性”的数据表中
这是它显示的错误
严重性:通知
消息:未定义的属性:stdClass::$totalcount
文件名:pages/studentgender.php
行号:20
回溯:
文件:C:\xampp\htdocs\libsystem\application\views\pages\studentgender.php 线路:20 函数:_error_handler
文件:C:\xampp\htdocs\libsystem\application\controllers\Students.php 线路:30 功能:查看
这是我的模特
public function studentCountGender(){
$this->db->select('studgender, COUNT(*) as totalcount, COUNT( case when studgender = "Male" then 1 else 0 end)
AS malecount, COUNT( case when studgender = "Female" then 1 else 0 end) AS femalecount');
$this->db->from('student');
$query = $this->db->get();
return $query->result();
}
}
这是我的控制器
public function studentList(){
$this->load->model('student_model');
$data['result'] = $this->student_model->studentCountGender();
$this->load->view('templates/header');
$this->load->view('templates/sidebar');
$this->load->view('pages/studentgender', $data);
}
这是我的看法
<thead>
<tr>
<th>Total No. of Students</th>
<th>Total Number of Males</th>
<th>Total Number of Females</th>
</tr>
</thead>
<tbody>
<?php
foreach($sample as $item){
echo '<tr>
<td>'.$item->totalcount.'</td>
<td>'.$item->malecount.'</td>
<td>'.$item->femalecount.'</td>
</tr>
</tbody>
';}?>
</table>
</div>
</div>
</div>
</div>
有人可以帮助我如何解析它并且它不会显示错误吗?我已经尝试调试了 2 个小时,这让我发疯了,非常感谢!!!
【问题讨论】:
-
你从哪里带来
$sample? -
你能把查询结果粘贴到这里吗?
-
将 $sample 替换为 $data['result'] 。我希望它会起作用。
-
只要做var_dump($data),你就会明白了
标签: php codeigniter