CodeIgniter为搜索结果添加分页

时间:2017-01-22 13:55:24

标签: php codeigniter pagination

我有一个带分页的数据表,我刚刚添加了一个搜索功能。目前我的搜索结果没有分页。我试图将我的索引中的配置添加到搜索功能,但它没有工作。我想知道我是否遗漏了什么。如果有人能看一眼,我会很感激吗?

控制器

public function index(){
        $config['base_url'] = base_url('/Control/Users');
        $config['total_rows'] = $this->UserModel->countUsers();
        $config['per_page'] = 30;
        $config['uri_segment'] = 3;
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['prev_link'] = '&lt;';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&gt;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="current"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['first_link'] = '&lt;&lt;';
        $config['last_link'] = '&gt;&gt;';
        $this->pagination->initialize($config);
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['datatable'] = $this->UserModel->getAllUsers($config["per_page"], $page);

        $data['links'] = $this->pagination->create_links();
        $data['counter'] = $config['total_rows'];
        $data['message']='';
        $this->load->view('control/controlMenu/navigationLink');
        $this->load->view('control/controlUsers/manageUsers',$data);
        $this->load->view('control/controlMenu/navigationJquery');
    }

    public function searchUser(){
        $inputsearchterm = $this->input->post('inputsearchterm');
        if($inputsearchterm!=""){
            $data['datatable'] = $this->UserModel->searchUser($inputsearchterm);
            $data['links'] = '';
            $data['counter'] = $this->UserModel->countSearchUser($inputsearchterm);
            $data['message']='';
            $this->load->view('control/controlMenu/navigationLink');
            $this->load->view('control/controlUsers/manageUsers',$data);
            $this->load->view('control/controlMenu/navigationJquery');
        }else{
            redirect('Control/Users');
        }
    }

查看

<?php echo form_open_multipart('Control/Users/searchUser','class="inputform"');?>
                             <div class="form-group">
                                <label for="inputsearchterm">Search</label>
                                <input type="text" class="form-control" id="inputsearchterm" name="inputsearchterm" placeholder="Search">
                            </div>
                            <button type="submit" class="btn btn-primary">Search</button>
                        <?php echo form_close(); ?>
                        <table class="table">
                            <thead>
                                <tr>
                                    <td>Name</td>
                                    <td>IC Number</td>
                                    <td>Passport Number</td>
                                    <td>Email</td>
                                    <td>Phone</td>
                                </tr>
                            </thead>
                            <tbody>
                                <?php 
                                    if(!empty($datatable)){
                                    foreach ($datatable as $dataitem):
                                    $id = $dataitem->id;
                                ?>
                                <tr>
                                    <td class="name"><?php echo $dataitem->firstname." "; ?><?php echo $dataitem->lastname; ?></td>
                                    <td class="icnumber"><?php echo $dataitem->icNumber; ?></td>
                                    <td class="passportnumber"><?php echo $dataitem->passportNumber; ?></td>
                                    <td class="email"><?php echo $dataitem->email; ?></td>
                                    <td class="phone"><?php echo $dataitem->phone; ?></td>
                                    <td><a class="edituser" href="<?php echo base_url();?>Control/UserDetail/<?php echo $id; ?>/<?php echo "true"; ?>"><i class="fa fa-pencil-square-o" aria-hidden="true"></i>Edit</a></td>
                                    <td><a class="deleteuser" href="<?php echo base_url();?>Control/Users/Users/deleteUser/<?php echo $id; ?>"><i class="fa fa-trash" aria-hidden="true"></i>Delete</a></td>
                                </tr>
                                <?php 
                                    endforeach;
                                    }
                                ?>
                            </tbody>
                        </table>
                        <p><?php echo $this->session->flashdata('Table'); ?></p>  
                        <?php echo $links; ?>
                        <p><?php echo $this->session->flashdata('Form'); ?></p>  
                    </div>

模型

public function searchUser($userdetail){
        $this->db->select('*');
        $this->db->from('Users');
        $this->db->like('firstname',$userdetail);
        $this->db->or_like('lastname',$userdetail);
        $this->db->or_like('email',$userdetail);
        $this->db->or_like('phone',$userdetail);
        $this->db->or_like('icNumber',$userdetail);
        $this->db->or_like('passportNumber',$userdetail);
        $query = $this->db->get();
        if($query->num_rows()>0){
            return $query->result();
        }else{
            return $query->result();
        }
    }

    public function countSearchUser($userdetail){
        $this->db->select('*');
        $this->db->from('Users');
        $this->db->like('firstname',$userdetail);
        $this->db->or_like('lastname',$userdetail);
        $this->db->or_like('email',$userdetail);
        $this->db->or_like('phone',$userdetail);
        $this->db->or_like('icNumber',$userdetail);
        $this->db->or_like('passportNumber',$userdetail);
        $query = $this->db->count_all_results();
        if($query>0){
            return $query;
        }else{
            return 0;
        }
    }

1 个答案:

答案 0 :(得分:0)

我认为您应该添加分页库或在模型中检查您的活动查询