我認爲你正在尋找:
$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