dtjpz48440 2016-08-15 05:33
浏览 25
已采纳

在codeigniter中返回函数方法中的多个值

This is my query counting rows in tblapplication

public function countallrecord() {
    $query = $this->db->get('tblapplication');

    return $query->num_rows();  
}

and this is the function to get all the data

public function getdata() {
    $query = $this->db->get('tblapplication');

    return $query->result();
}

Is there any way I can make this code on one function

I'm trying to pass it here:

public function Countandviewallrecord() {
    // returns both rows and count
}
  • 写回答

2条回答 默认 最新

  • douzhanrun0497 2016-08-15 05:39
    关注

    Just return it as an array. Include the results and count in their respective indices:

    public function get_records()
    {
        $result = $this->db->get('tblapplication');
        $data['results'] = $result->result();
        $data['count'] = $result->num_rows;
    
        return $data;
    }
    

    When accessing the model method in your controller, the usual:

    $data = $this->model_name->get_records();
    
    echo $data['count']; // whatever number this is
    if($data['count'] > 0) {
        foreach($data['results'] as $row) {
            echo $row->column_name; // etc blah blah ..
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询
  • ¥15 R语言建立随机森林模型出现的问题
  • ¥20 unity内置语言切换的按钮设置
  • ¥15 中级微观经济学,生产可能性边界问题
  • ¥15 TCP传输时不同网卡传输用时差异过大
  • ¥15 请各位看看我写的属于什么算法,或者有更正确的写法?
  • ¥15 html5 qrcode 扫描器
  • ¥15 爬取网页信息并保存需要完整代码