CodeIgniter COUNT有活动记录吗?

时间:2015-02-14 05:41:36

标签: php arrays codeigniter

我在codeigniter工作..我想算不上。下面具有相同order_no ..的行是我的代码。

public function get_my_orders() {
        $user_data = $this->session->all_userdata();
        $email = $user_data['user_email'];
            $this->db->select('*');
            $this->db->from('order_details');
            $this->db->where('email', $email);
            $this->db->group_by('order_no');
            $this->db->order_by('order_no', 'DESC');
            $query = $this->db->get();
            return $query->result();
    }

请帮助..

3 个答案:

答案 0 :(得分:1)

尝试将选择行更改为如下所示:

            $this->db->select('COUNT(*) as count');

您可以只访问一个名为count的变量,而不是像现在这样访问所有字段。要保持所有变量的可访问性并添加计数,请改为使用:

            $this->db->select('*, COUNT(*) as count');

可以随意更改count的小写名称,我只是以此为例。

答案 1 :(得分:1)

您可以使用$this->db->count_all_results()

 public function get_my_orders() {
    $user_data = $this->session->all_userdata();
    $email = $user_data['user_email'];
        //$this->db->select('*');// no need select as you only want counts
        $this->db->from('order_details');
        $this->db->where('email', $email);
        //$this->db->group_by('order_no');//no need group_by as you only want counts
        //$this->db->order_by('order_no', 'DESC');//no need order_by as you only want counts            
        return $this->db->count_all_results();;
}

答案 2 :(得分:0)

检查Codeigniter Manual

$query = $this->db->get();
return  $query->num_rows();  //Return total no. of rows here
  

$query->num_rows();将根据您的查询计算总有效记录回报。