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条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)