微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐