我认为你正在寻找:
$this->db->count_all('songs_tbl');
,或者如果你想要的不同之处在那里,你需要做这样的事情:
$this->db->select('song_id');
$this->db->distinct();
$this->db->from('songs_tbl');
$query = $this->db->get();
return $query->num_rows();
由于有/是?使用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();
}
编辑2
我建议的布局将采用无笨Smarty的东西沿着这些路线(未经测试):
型号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();
}
}
控制器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));
}
}
查看songs_index.html
<html><head></head><body>
Total Songs: <?php echo $total_songs ?>
</body></html>
哪里$歌曲从何而来? – karlipoppins
'$ songs'为空。 – xdazz
是因为$歌曲吗?是的,我忘了delcare $歌曲,我应该申报什么? – spotlightsnap