2016-08-10 5 views
2

나는 codeigniter에서 같은 테이블의 전체 데이터와 전체 행 수를 가져 오는 2 가지 경우가 있습니다. 전체 행 수를 가져올 수있는 방법이 있는지 알고 싶습니다. , 전체 데이터를 하나 개의 코드를 통해 동일한 테이블 3 개 최신 삽입 레코드codeigniter의 데이터베이스에서 데이터 가져 오기

두 경우 모두에 대한

컨트롤러 코드 (나는 다른 매개 변수를 별도로 각각의 경우에 대해이를 적용하고있다)에 대해서 설명 같다

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

1) 전자 가져 오기 CodeIgniter의에서 테이블에서 ntire 데이터

모델 코드

public function getreq() 
    { 
     $this->db->where('status','pending'); 
     $query=$this->db->get('instanthire'); 
     return $query->result(); 
    } 

보기 코드 CodeIgniter의에서 테이블에서 행의 수를 가져 오기 위해

foreach ($instant_req as $perreq) 
    { 
     echo $perreq->fullname; 
     echo "<br>"; 
    } 

2)

public function getreq() 
    { 
     $this->db->where('status','pending'); 
     $query=$this->db->get('instanthire'); 
     return $query->num_rows(); 
    } 

코드보기

echo $instant_req; 

답변

2

당신은 행 한 번 총에 당신에게 모든 데이터를 제공 하나의 기능을 할 수 있습니다 , 전체 데이터 및 최근 삽입 된 레코드 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); 
} 

내가이 테이블이있는 반면 성능이 내려갈 수 있도록이 세 가지 DB를 쿼리를 할 것이라고 생각

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; 
0

여기서 원시 쿼리가 작동합니다.

$resultSet = $this->db->query("select * from table_name"); 
$queryCount = count($resultSet); 
0

다음은 내가 생각할 수있는 간단한 해결책입니다.하지만 당신이 나를 향상 시키길 원한다면 할 수 있습니다.

첫 번째 코드 (모델)를 고수하고 뷰에서 얼마나 많은 항목이 반복되는지 계산하십시오.

$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 
} 

정말이 시도했지만 내 머리 위에되지 않은이 다른 솔루션입니다

, 배열을 반환 : 그냥 날

EDIT 알려 나는 그것이 효과가 있다고 생각한다.

1

기능

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(); 

CodeIgniter Database Documentation

1

이 논리보십시오 :

모델 코드 :

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; 
0

모델 :

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; 
} 
+0

많은 기록들 –

관련 문제