2014-06-19 4 views
0

일부 필터 옵션과 페이지 매김을 사용하여 데이터 테이블을 만들려고합니다. (또한 나중에 정렬 열을 추가하려는 경우).CodeIgniter : 필터 행이있는 페이지 매김

페이지 1에서 데이터를 필터링하려고하면 정상적으로 작동합니다. 그러나 나는 다른 페이지/s의 오전과 데이터를 필터링 할 때이

public function employees() 
{ 
    $this->data['title'] = '<i class="fa fa-users"></i> ' . lang('emp_all'); 

    $config    = array(); 
    $config['base_url'] = base_url() . 'admin/hr/employees'; 
    $config['total_rows'] = $this->employees_model->row_count(); 
    $config['per_page'] = 10; 
    $config['uri_segment'] = 4; 

    $this->pagination->initialize($config); 

    $page     = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0; 
    $this->data['results'] = $this->employees_model->fetch_rows($config['per_page'], $page); 
    $this->data['links'] = $this->pagination->create_links(); 

    $this->load->view('hr/employees/index', $this->data); 
} 

보기

public function fetch_rows($limit, $start) 
{ 
    $emp_id = $this->input->post('emp_id'); 
    $this->db->like('employee_id',$emp_id); 

    $emp_fname = $this->input->post('emp_fname'); 
    $this->db->like('first_name',$emp_fname); 

    $emp_login = $this->input->post('emp_login'); 
    $this->db->like('login_id',$emp_login); 

    $emp_position = $this->input->post('emp_position'); 
    $this->db->like('position',$emp_position); 

    $this->db->limit($limit, $start); 
    $query = $this->db->get($this->_table_name); 

    var_dump($query); 

    if ($query->num_rows() > 0) { 
     foreach ($query->result() as $row) 
     { 
      $data[] = $row; 
     } 
     return $data; 
    } 
    return FALSE; 
} 

컨트롤러에게 invalid argument supplied for foreach()

모델에게 오류를

을주는 것보다
<div class="form-group"> 
    <div class="row"> 
     <?= form_open(); ?> 
     <div class="col-xs-1"> 
      <?= form_input(array('name' => 'emp_id', 'id' => 'emp_id', 'class' => 'form-control', 'placeholder' => 'Employee ID')); ?>         
     </div> 
     <div class="col-xs-2"> 
      <?= form_input(array('name' => 'emp_fname', 'id' => 'emp_fname', 'class' => 'form-control', 'placeholder' => 'First Name')); ?>         
     </div> 
     <div class="col-xs-1"> 
      <?= form_input(array('name' => 'emp_login', 'id' => 'emp_login', 'class' => 'form-control', 'placeholder' => 'Login Id')); ?>         
     </div> 
     <div class="col-xs-2"> 
      <?= get_positions('dropdown', 'emp_position'); ?>         
     </div> 
     <div class="col-xs-1"> 
      <?= form_submit('filters', 'Go', 'class="btn-default btn"'); ?> 
     </div> 
     <div class="col-xs-2"> 

     </div> 

     <?= form_close(); ?> 
    </div> 
</div><!-- End filter form --> 

<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered datatables" id=""> 
    <thead> 
     <tr class="active"> 
      <?= is_system_admin() ? '<th class="center">ID<span>*</span></th>' : NULL; ?> 
      <th>Photo</th> 
      <th>Employee ID</th> 
      <th>Name</th> 
      <th>Position</th> 
      <th>Login ID</th> 
      <th>Email</th> 
      <th>Status</th> 
     </tr> 
    </thead> 
    <tbody> 
     <?php 
     if (count($results) > 0): foreach ($results as $data):      
       echo '<tr>'; 
       echo is_system_admin() ? '<td class="center">' . $data->id . '</td>' : NULL; 
       echo '<td><img src="' . get_employee_snap($data->employee_id, $data->employee_snap) . '" alt="snap" width="64"/></td>'; 
       echo '<td>' . $data->employee_id . '</td>'; 
       echo '<td>' . $data->first_name . ' ' . $data->last_name . '</td>'; 
       echo '<td>' . get_position_by_code($data->position) . '</td>'; 
       echo '<td>' . $data->login_id . '</td>'; 
       echo '<td>' . $data->email . '</td>'; 
       echo '<td>' . $data->employment_status . '</td>'; 
       echo '</tr>'; 
      endforeach; 
     else: 
      echo show_alert('No Postions record found in database.', FALSE, 'warning'); 
     endif; 
     ?>          
    </tbody> 
</table><!--end table--> 
<?= $links; ?> 

이 문제를 해결하는 방법을 내가 어떤 페이지에 상관없이 필터링 할 수 있으며, 또한 정렬 할 수있는 열을 만들고 싶습니다.

참고 : 필터 결과도 마찬가지로 페이지 번호를 업데이트하고 싶습니다.

백만 덕분에 ... :)

+0

사용할 수 있습니까? 모델 또는보기 파일에 있습니까? foreach 루프가 비어 있지 않기 전에'$ query-> result' 덤프를 시도 했습니까? – Bora

+0

보기'foreach()'. 내보기 코드 테이블 –

답변

1

당신은 단순히

return $query->result_array(); 

을 사용할 수 있습니다 그리고 난 아무 결과가 발견되지 않고 FALSE가없는 배열 때문에 foreach에 오류가 발생합니다 경우 $this->data['results']이 거짓이 될 것이라는 점을 발견했다.

참고 : 결과 집합이 비어 있으면 빈 배열이됩니다.

그래서 대신

if ($query->num_rows() > 0) { 
     foreach ($query->result() as $row) 
     { 
      $data[] = $row; 
     } 
     return $data; 
    } 
    return FALSE; 

의 모델에 당신이 오류가 않았다 foreach는

return $query->result_array(); 
+0

감사합니다 ..하지만 시도했을 때'$ message-> employee_id'와 같은 모든 열에 대해'Message : non-object의 속성을 얻으려고 시도 중 '오류가 발생했습니다. –

+0

@CodeLover 그 배열 ('$ query-> result_array() ;) 그래서'$ data-> employee_id' 대신'$ data [ 'employee_id']'를 사용해야합니다 –

+0

오! 너무 바보 같아요. 한 번 사용해 보도록하겠습니다. –

1

변경과 같은 모델 : 데이터를로드 할 수 없습니다 쿼리의 $data가 정의되지 않은 고려할 경우

if ($query->num_rows() > 0) { 
    $data = array(); 
    foreach ($query->result() as $row) 
    { 
     $data[] = $row; 
    } 
    return $data; 
} 

인해 어떤 이유에. foreach는 실행되지 않기 때문에. 대신

 foreach ($query->result() as $row) 
     { 
      $data[] = $row; 
     } 
     return $data; 

+0

에서도 확인할 수 있습니다. 코드도 작동하지만 필터 결과에 따라 페이지 매김에 영향을 주려고합니다. 그래서 결과 행의 개수가 페이지 매김보다 작은 페이지 당 페이지 수보다 작 으면 –

+0

에 표시되어야합니다. 그렇다면 문제는'$ this-> uri-> segment (4)'부분에 있다고 생각합니다. 'fetch_rows' 메소드를 통해 올바른 값을 모델에 전달했는지 확인하십시오. – Eranda

+0

꽤 괜찮습니다. 모든 것이 작동하지만 다른 페이지에는 필터 만 있습니다. –

관련 문제