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