在Codeigniter中从数据库中获取数据 [英] Fetch data from database in codeigniter
问题描述
我有2种情况,我要在codeigniter中获取同一表的全部数据和行总数,我想知道有一种方法可以获取总行数,整个数据和3个最新行通过一个代码从同一张表中插入记录
I have 2 cases where i am fetching the entire data and total number of rows of a same table in codeigniter, I wish to know that is there a way through which i can fetch total number of rows, entire data and 3 latest inserted records from the same table through one code
这两种情况的控制器代码如下(尽管我分别针对每种情况使用了不同的参数)
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中的表中获取全部数据
1) to fetch the entire data from a table in 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中的表中获取行数
2) to fetch number of rows from a table in codeigniter
public function getreq()
{
$this->db->where('status','pending');
$query=$this->db->get('instanthire');
return $query->num_rows();
}
查看代码
echo $instant_req;
推荐答案
您只能创建一个函数,一次给您所有数据,总行数,全部数据和3条最新插入的记录
You can make only one function that gives you the all data at once total number of rows, entire data and 3 latest inserted records
例如在模型中
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;
这篇关于在Codeigniter中从数据库中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!