php - 在 codeigniter 中从数据库中获取数据

标签 php mysql 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);
}

在 Controller 仪表板功能中

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

在 View 中

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;

关于php - 在 codeigniter 中从数据库中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38867937/

相关文章:

php - remote_addr 不返回 IPv4 地址

php - MySQL/PHP 中的时区偏移量(和 st/dst 更改)

php - 如何/在哪里将参数从 View 传递到 CodeIgniter 中的 Controller ?

php - 文件未在 codeigniter 中上传

error-handling - PHP error_log错误到MySQL

php - 在 PHP 中使用 XML-Reader 进行 XML 验证

php - 在 PHP 中的特定值之后在数组中添加新元素?

php - 从多对多表中检索分组

mysql - 将上一个插入的id插入到MYSQL中的下一个插入中

mysql - 显示达到特定计数阈值的不同用户