我有 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/