Reputation: 1
I'm trying to fetch data from my database to a data table with table headers "Total No. of Students | Male | Female"
This is the error it showed
Severity: Notice
Message: Undefined property: stdClass::$totalcount
Filename: pages/studentgender.php
Line Number: 20
Backtrace:
File: C:\xampp\htdocs\libsystem\application\views\pages\studentgender.php Line: 20 Function: _error_handler
File: C:\xampp\htdocs\libsystem\application\controllers\Students.php Line: 30 Function: view
This is my model
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();
}
}
This is my controller
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);
}
This is my view
<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>
Can someone help me how to parse it and it will not show the error? I've been trying to debug this for 2 hours and it's making me insane thanks alot!!!
Upvotes: 0
Views: 1912
Reputation: 233
Please try the below code hope this help you. may you don't have the result or pass the $data in incorrect view please check accordingly.
<?php
if(isset($result)&& !empty($result)){
foreach($result as $item){
echo "<tr>
<td>".$item->totalcount."</td>
<td>".$item->malecount;"</td>
<td>".$item->femalecount."</td>
</tr>";
}
}
?>
Upvotes: 0
Reputation: 2249
Try this, will work for you.
Your controller:
public function __construct()
{
parent::__construct();
$this->load->model('student_model');
}
public function studentList(){
$data = [];
$data['result'] = $this->student_model->studentCountGender();
$this->load->view('templates/header');
$this->load->view('templates/sidebar');
$this->load->view('pages/studentgender', $data);
}
Your model:
<?
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();
}
Your view:
foreach($result as $item){
echo '<tr>
<td>'.$item->totalcount.'</td>
<td>'.$item->malecount.'</td>
<td>'.$item->femalecount.'</td>
</tr>
</tbody>
}
Greetings!
Upvotes: 0
Reputation: 79
You are passing $data['result']
to the view , Then how do you get result in $sample
as $item
in foreach
. Please check again.
Upvotes: 0
Reputation: 1336
Try this code once.
<?php
foreach($result as $item){
echo "<tr>
<td>{$item->totalcount}</td>
<td>{$item->malecount}</td>
<td>{$item->femalecount}</td>
</tr>";
}
?>
Upvotes: 3