下拉筛选器不起作用。

3
我在Stackoverflow上看到了一个筛选器的示例,我想在我的CodeIgniter上实现它,但是当我点击“Go”按钮时,它没有对其进行筛选。它总是返回找不到用户,这是什么问题?
控制器
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
我建议您检查方法是否接收到必要的参数。正如@hakra所说,我们不会调试代码,我们需要错误来找到解决方案。=) - user745235
1个回答

1

请检查您的模型中的getAllFilterUsers函数。$offset的值为5,$count的值为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');
            }           
    }

就是这样


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接