Codeigniter - 计数 table 不工作

Codeigniter - count all on the table not working

你能帮我解决这个问题吗?我正在尝试计算我的 table 中有多少,因为它没有在我的视图中显示结果。

控制器

  public function enrollment_summary()
  {
    $data['title'] = 'Enrollment Summary';
    $this->load->model('report_model');
    $data['query'] = $this->report_model->get_students();
    $this->load->view('report_enrollment_summary', $data);
  }

型号

 <?php
class report_model extends CI_Model
{
  public function __construct()
  {
    parent::__construct();
    $this->load->database();
  }
  public function get_students()
  {
    $query = $this->db->count_all('students');
  }
}

查看

<body>
  <h1>Enrollment Summary</h1>
  <?php echo $query; ?>
</body>

您的 get_students() 没有 return 任何东西?

所以你需要添加一个return。

您当前的方法...

  public function get_students()
  {
    $query = $this->db->count_all('students');
  }

变成

  public function get_students()
  {
    $query = $this->db->count_all('students');
    return $query;
  }

  public function get_students()
  {
    return $this->db->count_all('students');
  }