我想要做的是我想从我的数据库中计算名为"songs_tbl"的表中的总记录数.所以我在控制器中写了这个函数.
private function getHeaderInfo()
{
$total_songs = $songs->count('distinct songs_tbl.song_id');
$this->mysmarty->assign('total_songs',$total_songs);
}
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误
致命错误:调用非对象中的成员函数count()
有什么建议吗?谢谢.
带着敬意,
我想你正在寻找:
$this->db->count_all('songs_tbl');
Run Code Online (Sandbox Code Playgroud)
或者如果你想要那里的独特,你需要做这样的事情:
$this->db->select('song_id');
$this->db->distinct();
$this->db->from('songs_tbl');
$query = $this->db->get();
return $query->num_rows();
Run Code Online (Sandbox Code Playgroud)
有/有吗?使用count_all_results()函数和DISTINCT的问题
编辑
我从来没有使用过smarty,但基于问题中的代码我想象这样的东西可能有用,如果我错了,请纠正我:
private function getHeaderInfo()
{
$total_songs = get_all_songs();// This function should be called through a model
$this->mysmarty->assign('total_songs',$total_songs);
}
function get_all_songs(){ //THIS SHOULD BE IN A MODEL
$this->db->select('song_id');
$this->db->distinct();
$this->db->from('songs_tbl');
$query = $this->db->get();
return $query->num_rows();
}
Run Code Online (Sandbox Code Playgroud)
编辑2
我建议的布局将是使用CodeIgniter而不是聪明的这些行(UNTESTED):
型号Song.php
class Song extends CI_Model {
//Constructor and other functions
function count_all_songs(){
$this->db->select('song_id');
$this->db->distinct();
$this->db->from('songs_tbl');
$query = $this->db->get();
return $query->num_rows();
}
}
Run Code Online (Sandbox Code Playgroud)
Controller Songs.php
class Song extends CI_Controller {
//Constructor and other functions
function index(){ //This could be any page
$this->load->model('Song'); //Could be in constructor
$total_songs = $this->Song->count_all_songs();
$this->load->view('songs_index.html', array('total_songs' => $total_songs));
}
}
Run Code Online (Sandbox Code Playgroud)
查看songs_index.html
<html><head></head><body>
Total Songs: <?php echo $total_songs ?>
</body></html>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12110 次 |
| 最近记录: |