php – 从codeigniter中的数据库中获取数据

我有2个案例,我在codeigniter中获取整个数据和同一个表的总行数,我想知道有一种方法可以从中获取总行数,整个数据和3个最新插入的记录同一个表通过一个代码

Controller code for both cases is as given below (although i am applying it for each case seperately with different parameters)

public function dashboard()
    {
        $data['instant_req'] = $this->admin_model->getreq();
        $this->load->view('admin/dashboard',$data);
    }

1)从codeigniter中的表中获取整个数据

型号代码

public function getreq()
    {
        $this->db->where('status','pending');
        $query=$this->db->get('instanthire');
        return $query->result();
    }

查看代码

foreach ($instant_req as $perreq) 
    {
        echo $perreq->fullname;
        echo "<br>";
    }

2)从codeigniter中的表中获取行数

public function getreq()
    {
        $this->db->where('status','pending');
        $query=$this->db->get('instanthire');
        return $query->num_rows();
    }

查看代码

echo $instant_req;

解决方法:

您只能使用一个函数同时为所有数据提供总行数,整数数据和3个最新插入记录

例如在模型中

public function getreq()
{
    $this->db->where('status','pending');
    $query=$this->db->get('instanthire');
    $result=$query->result();
    $num_rows=$query->num_rows();
    $last_three_record=array_slice($result,-3,3,true);
    return array("all_data"=>$result,"num_rows"=>$num_rows,"last_three"=>$last_three_record);
}

在控制器仪表板功能

public function dashboard()
{
    $result = $this->admin_model->getreq();
    $this->load->view('admin/dashboard',$result);
}

在视野中

foreach ($all_data as $perreq) 
{
    echo $perreq->fullname;
    echo "<br>";
}
//latest three record
foreach ($last_three as $perreq) 
{
    echo $perreq->fullname;
    echo "<br>";
}
//total count
echo $num_rows;

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...