CodeIgniter与搜索分页并不显示第二页

时间:2016-12-19 08:10:39

标签: php codeigniter pagination

我试图了解为什么当我转到第2页时,我对搜索的分页会一直恢复到默认状态。

我已经看过一些例子,这些例子已经说过我的搜索条件在会话数据中,以便搜索可以保留,但到目前为止,我没有运气这么做。我的代码部分来自此网站:https://dzone.com/articles/how-paginate-search-results 我不确定这种方法有多正确。

这是我到目前为止所拥有的

控制器:

public function date_function(){
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
    $date_start = $this->table_DateStartHandler($this->input->post('date_start'));
    $date_end = $this->table_DateEndHandler($this->input->post('dateend'));
    $current_status = $this->table_StatusHandler($this->input->post('current_status'));

    $config['base_url'] = base_url('/Report/date_function');
    $config['total_rows'] = $this->ControlModel->count_Date($date_start,$date_end,$current_status);
    $config['per_page'] = 40;
    $config['uri_segment'] = 4;
    $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(4)) ? $this->uri->segment(4) : 0;
    $message['datatable'] = $this->ControlModel->getSubmissions_Date($config["per_page"], $page,$date_start,$date_end,$current_status);

    $message['links'] = $this->pagination->create_links();

    $message['counter'] = $config['total_rows'];

    //$message['datatable'] = $this->ControlModel->getSubmissions();
    $message['message'] = '';
    $this->load->view('control_report',$message);
}

检查会话中是否有userdata的函数

public function table_StatusHandler($current_status){
    if($current_status){
        $this->session->set_userdata('current_status', $current_status);
        return $current_status;
    }elseif($this->session->set_userdata('current_status')){
        $current_status = $this->session->userdata('current_status');
        return $current_status;
    }else{
        $current_status = "";
        return $current_status;
    }
}

public function table_DateStartHandler($date_start){
    if($date_start){
        $this->session->set_userdata('date_start', $date_start);
        return $date_start;
    }elseif($this->session->set_userdata('date_start')){
        $date_start = $this->session->userdata('date_start');
        return $date_start;
    }else{
        $date_start = "";
        return $date_start;
    }
}

public function table_DateEndHandler($date_end){
    if($date_end){
        $this->session->set_userdata('date_end', $date_end);
        return $date_end;
    }elseif($this->session->set_userdata('date_end')){
        $date_end = $this->session->userdata('date_end');
        return $date_end;
    }else{
        $date_end = "";
        return $date_end;
    }
}

希望有人可以提供帮助 谢谢

1 个答案:

答案 0 :(得分:0)

将{id}作为searchList提供给您的表单。然后通过粘贴视图来尝试此代码

<script type="text/javascript">
    var baseURL = '<?php echo base_url(); ?>';
    jQuery(document).ready(function(){
        jQuery('ul.pagination li a').click(function (e) {
            e.preventDefault();            
            var link = jQuery(this).get(0).href;            
            var value = link.substring(link.lastIndexOf('/') + 1);
            jQuery("#searchList").attr("action", baseURL + "Report/date_function/" + value);
            jQuery("#searchList").submit();
        });
    });
</script>

在您的控制器功能中:

public function date_function(){
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
    $date_start = $this->table_DateStartHandler($this->input->post('date_start'));
    $date_end = $this->table_DateEndHandler($this->input->post('dateend'));
    $current_status = $this->table_StatusHandler($this->input->post('current_status'));

    // You Pagination Code


    $message["date_start"] = $this->input->post('date_start');
    $message["dateend"] = $this->input->post('dateend');
    $message["current_status"] = $this->input->post('current_status');

    $this->load->view('control_report',$message);
}

我在数组$message中发送的这三个参数,在加载视图时将它们分配给您输入控件。