2012-08-24 44 views
3

我在Stackoverflow上看到了一個過濾器示例,我試圖在我的codeigniter上實現它,但是當我單擊go按鈕時它不會過濾它。它總是返回我no user found這是什麼問題?下拉過濾器不工作

控制器

public function filter($page=0,$offset=5,$search=''){ 

     $user = $this->session->userdata('user_id'); 
     $position = $this->session->userdata('user_position'); 



     if($position =='admin'){ 

      $search = $this->input->post('search'); 
      $row = $this->m_user->getAllFilterUsers($offset,$page,$search); 
      $data['usersTable'] = $row->result(); 
      $data['pagination'] = $this->m_user->getUsersFilterPages($search); 
      $data['offset'] = $offset; 
      $data['page'] = $page; 
      $data['search'] = $search; 
      $data['title'] = 'Manage Users'; 
      $this->load->view('vadminuserfilter',$data); 


     } 
     else{ 
      $this->session->set_flashdata('error','Page Not Found.'); 
      redirect('cuser/displayClientPage'); 
      return; 
     } 



    } 

型號

public function getAllFilterUsers($offset,$count,$search){ 

     if($search!=''){ 
      $this->db->where('user_position','client'); 
      $this->db->where('user_status',$search); 
     } 
         $this->db->where('user_position','client'); 
         $this->db->where('user_status','active'); 
         $this->db->order_by('user_id', 'desc'); 
      $UsersQuery = $this->db->get('tb_user',$offset,$count); 

      if($UsersQuery->num_rows>0){ 
       return $UsersQuery; 
      } 

      else{ 



       $this->session->set_flashdata('message','No User Found'); 
       redirect('cuser/filter','refresh'); 
      }   
    } 


    public function getUsersFilterPages($search){ 

     $this->db->where('user_position','client'); 
     $this->db->where('user_status',$search); 
     $Pagesquery = $this->db->get('tb_user'); 
      $config['base_url'] = site_url('cuser/filter'); 
      $config['total_rows']= $Pagesquery->num_rows(); 
      $config['per_page'] = 5; 

      $config['first_link'] = 'First'; 
      $config['prev_link'] = 'Previous'; 
      $config['next_link'] = 'Next'; 
      $config['last_link'] = 'Last'; 
      $this->pagination->initialize($config); 


    return $this->pagination->create_links(); 

    } 

查看

<?php echo form_open('cuser/filter'); ?> 
Filter By: 
<?php $dropdown = array('active'=>'active','inactive'=>'inactive');?> 
<?php echo form_dropdown('search', $dropdown); ?> 
<input name="Submit" type="submit" class="button" <?php echo form_submit('submit','Go');?> 
<?php echo form_close(); ?> 
+4

行不通的標題問題。不,我們不調試你的代碼。請先找出錯誤的原因。你也在做錯誤檢查嗎? – hakre

+0

我建議您檢查您的方法是否正在接收必要的參數。正如@hakra所說,我們不會調試代碼,我們需要找到一個解決方案的錯誤。 =) – Gerep

回答

1

您只需查看模型getAllFilterUsers功能。的$偏移值是5和$計數值爲0 但你卻用..

this->db->where('user_position','client'); 
$this->db->where('user_status','active'); 
$this->db->order_by('user_id', 'desc'); 
$UsersQuery = $this->db->get('tb_user',$offset,$count); 

這意味着..

Select * from tb_user where user_position = 'client' and 'user_status' = 'active' order by user_id desc limit 5,0 

此查詢總是返回空集 您的查詢應該是..

Select * from tb_user where user_position = 'client' and 'user_status' = 'active' order by user_id desc limit 0,5 

所以,你的代碼段應

this->db->where('user_position','client'); 
$this->db->where('user_status','active'); 
$this->db->order_by('user_id', 'desc'); 
$UsersQuery = $this->db->get('tb_user',$count,$offset); 

最後的功...

public function getAllFilterUsers($offset,$count,$search){ 

     if($search!=''){ 
      $this->db->where('user_position','client'); 
      $this->db->where('user_status',$search); 
     } 
         this->db->where('user_position','client'); 
         $this->db->where('user_status','active'); 
         $this->db->order_by('user_id', 'desc'); 
      $UsersQuery = $this->db->get('tb_user',$count,$offset); 

      if($UsersQuery->num_rows>0){ 
       return $UsersQuery; 
      } 

      else{ 



       $this->session->set_flashdata('message','No User Found'); 
       redirect('cuser/filter','refresh'); 
      }   
    } 

完蛋了