【问题标题】:Code Igniter Count FunctionCodeigniter 计数函数
【发布时间】:2011-11-16 10:05:39
【问题描述】:

我想做的是从我的数据库中计算名为“songs_tbl”的表中的总记录。所以我在控制器中编写了这个函数。

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

我收到了这个错误

致命错误:在非对象中调用成员函数 count()

有什么建议吗?谢谢。

问候,

【问题讨论】:

  • 是因为 $songs 吗?是的,我忘了删除 $songs,我应该声明什么?

标签: php codeigniter


【解决方案1】:

我想你正在寻找:

$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

我建议的布局将是使用 CodeIgniter 没有 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();
    }
}

控制器 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));
    }
}

查看 song_index.html

<html><head></head><body>
    Total Songs: <?php echo $total_songs ?>
</body></html>

【讨论】:

  • 谢谢。但是我正在使用 smarty,如何在模板端执行该功能?
  • $total_songs = get_all_songs();// 这个函数应该通过模型调用 $this->mysmarty->assign('total_songs',$total_songs);这两行应该与函数 get_all_songs 在同一个模型文件中?如果我放入相同的模型文件,我在执行时会得到空白页。
  • 我应该在控制器中放入什么?我将上面的代码放在我的模型文件中,并在模板端 执行这样的函数,没有任何反应。我也没有看到任何计数或错误。
  • 感谢它的作品。我只是将此行更改为 $total_songs = $this->smarty->assign("total_songs", number_format($this->Song->count_all_songs("")));
【解决方案2】:

您可以查询表并从表本身请求计数,如下所示:

$result = mysql_query(SELECT count(*) FROM songs_tbl);

【讨论】:

    【解决方案3】:

    试试这个

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

    它允许您确定特定表中的行数。

    【讨论】:

      【解决方案4】:

      你可以用这个

      $query = $this->db->get('distinct');
              if($query->num_rows())
              {
                  return $query->num_rows();
              }else{
                  return 0;
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-06
        • 2016-10-09
        • 2019-02-14
        • 1970-01-01
        • 2014-01-23
        • 1970-01-01
        • 2013-08-28
        • 1970-01-01
        相关资源
        最近更新 更多