dongni8969 2011-11-16 10:05
浏览 68
已采纳

代码点火器计数功能

What I want to do is I want to count the total records from the table called "songs_tbl" from my database. So I wrote this function in controller.

private function getHeaderInfo()
{
         $total_songs = $songs->count('distinct songs_tbl.song_id');
         $this->mysmarty->assign('total_songs',$total_songs);
}   

I got this error

Fatal error: Call to a member function count() on a non-object in

Any suggestion ? Thank you.

With Regards,

  • 写回答

4条回答 默认 最新

  • drpsrvu85668 2011-11-16 10:08
    关注

    I think you are looking for:

    $this->db->count_all('songs_tbl');
    

    or if you want the distinct in there you will need to do something like this:

    $this->db->select('song_id');
    $this->db->distinct();
    $this->db->from('songs_tbl');
    $query = $this->db->get();
    return $query->num_rows();
    

    As there is/was? an issue with using count_all_results() function and DISTINCT

    EDIT

    I have never used smarty but based on the code in the question I imagine something like this might work, please correct me if I am wrong:

    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();
    }
    

    Edit 2

    My suggested layout would be something along these lines (UNTESTED) using CodeIgniter WITHOUT smarty:

    Model 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();
        }
    }
    

    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));
        }
    }
    

    View songs_index.html

    <html><head></head><body>
        Total Songs: <?php echo $total_songs ?>
    </body></html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程