[英]Fetch data from database in codeigniter
我有2種情況,我要在codeigniter中提取同一表的全部數據和行總數,我想知道那是一種方法,可以從中獲取行總數,整個數據和3個最新插入的記錄通過一個代碼在同一張桌子上
兩種情況的控制器代碼如下(盡管我分別使用不同的參數將其應用於每種情況)
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;
原始查詢可能在這里起作用。
$resultSet = $this->db->query("select * from table_name");
$queryCount = count($resultSet );
功能
function getData($limit = 0){
//Create empty array
$data = [];
//Where clause
$this->db->where('status','pending');
//Order Data based on latest ID
$this->db->order_by('id', 'DESC');
if($limit != 0){
$this->db->limit($limit);
}
//Get the Data
$query = $this->db->get('instanthire');
$data['count'] = $query->num_rows();
$data['result'] = $query->result();
return $data;
}
呼叫
//Last 3 Inserted
$data = getData(3);
//All Data
$data = getData();
試試這個邏輯:
型號代碼:
public function getreq()
{
$this->db->where('status','pending');
$this->db->order_by('id', 'DESC'); //actual field name of id
$query=$this->db->get('instanthire');
return $query->result();
}
控制器代碼:
public function dashboard()
{
$data['instant_req'] = $this->admin_model->getreq();
$data['total_record'] = count($data['instant_req']);
$this->load->view('admin/dashboard',$data);
}
查看代碼:
$i=0;
foreach ($instant_req as $perreq)
{
if($i<3){
echo $perreq->fullname;
echo "<br>";
}
$i++;
}
Echo 'Total record : '.$total_record;
這是我首先想到的一個簡單解決方案,但是如果您希望我有所改進,我可以。
只需堅持使用您的第一個代碼(模型),然后在視圖中計算迭代了多少個項目。
$count = 0;
foreach ($instant_req as $perreq)
{
echo $perreq->fullname;
echo "<br>";
$count++;
}
echo $count;
我還缺少什么嗎? 讓我知道
編輯:
這是另一個解決方案,返回一個數組
public function getreq()
{
$this->db->where('status','pending');
$query=$this->db->get('instanthire');
$data['results'] = $query->result();
$data['count'] = $query->num_rows();
return $data
}
我不是很自信,也沒有真正嘗試過,但是在我的頭上,我認為它可以工作。
模型:
public function getreq()
{
$res = $this->db->order_by("<place column primary id>","desc")->get_where('instanthire',['status'=> 'pending']);
$latest_3 = [];
if(count($res)){
$i=1;
foreach($res as $r){
$latest_3[]=$r;
if($i == 3)
break;
$i++;
}
}
$arr = [
'latest_3' => $latest_3,
'count' => count($res),
'total_result' => $res,
];
return $arr;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.