使用codeigniter 3 paginaiton

时间:2016-04-20 11:09:42

标签: php codeigniter validation pagination

我有一个搜索表单,它接受7个不同的输入并使用这些值来查询来自5个不同表的数据。提取的行将返回到CodeIgniter控制器,以便在视图中显示,但具有分页。

除了其他特定于输入的验证之外,我还检查用户提供的7个字段中至少有一个值用于搜索。如果没有,那么我们就会抛出错误。

以下代码工作正常,它查询数据并显示分页但是当我点击分页链接的第2页时它会中断,因为然后控制器重新加载搜索表单并执行失败的表单验证。

控制器代码,用于执行验证并获取数据和分页:

public function search_metadata_database()
{
    $arr = array();
    $data = $this->input->post();

    foreach ($data as $key => $value)
    {
        if($value)
        {
            $arr[$key] = $value;
        }
    }

    if(count($arr) < 1)
    {
        $this->session->set_flashdata('metadata_search_message', 'Please provide atleast one input!');
        redirect('Metadata/search_metadata_view');
    } 
    else 
    {
        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('pages/metadataview/metadata_search');
            $this->load->view('pages/metadataview/metadata_form');
        }
        else
        {
            foreach ($arr as $key => $value)
            {
                $arr[$key] = $this->get_master_id_by_name($key,$value);
            }

            $this->load->helper('url');
            $this->load->library('pagination');
            $config['base_url'] = site_url('Metadata/search_metadata_database');
            $config['total_rows'] = $this->metadata_model->fetch_metadata_record_count($arr);
            $config['per_page'] = "2";
            $config["uri_segment"] = 2;
            $choice = $config["total_rows"] / $config["per_page"];
            $config["num_links"] = floor($choice);
            $config['full_tag_open'] = '<ul class="pagination">';
            $config['full_tag_close'] = '</ul>';
            $config['first_link'] = false;
            $config['last_link'] = false;
            $config['first_tag_open'] = '<li>';
            $config['first_tag_close'] = '</li>';
            $config['prev_link'] = '&laquo';
            $config['prev_tag_open'] = '<li class="prev">';
            $config['prev_tag_close'] = '</li>';
            $config['next_link'] = '&raquo';
            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';
            $config['last_tag_open'] = '<li>';
            $config['last_tag_close'] = '</li>';
            $config['cur_tag_open'] = '<li class="active"><a href="#">';
            $config['cur_tag_close'] = '</a></li>';
            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';

            $this->pagination->initialize($config);
            $data['page'] = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
            $data['songList'] 
            = $this->metadata_model->fetch_metadata_record($arr,$config["per_page"], $data['page']);
            $data['pagination'] = $this->pagination->create_links();
            $this->load->view('pages/metadataview/metadata_search');
            $this->load->view('pages/metadataview/metadata_pagination',$data);
        }
    }   
}

我没有显示第二页分页(接下来的两行),而是被重定向到以下视图:

$this->load->view('pages/metadataview/metadata_search');
$this->load->view('pages/metadataview/metadata_form');

连同消息:'请提供至少一个输入!'

如何在浏览分页链接时跳过表单验证,同时在有人点击搜索按钮时使其工作?

1 个答案:

答案 0 :(得分:0)

首先,只需在应用程序中创建一个库&gt;库文件夹

class MY_Pagination extends CI_Pagination {

    public function __construct() {
        parent::__construct();
    }

    public function curr_page() {
        return $this->cur_page;
    }
}

之后,只需在控制器中使用以下代码即可。

if($this->form_validation->run() == FALSE && $this->curr_page <= 1)
        {
            $this->load->view('pages/metadataview/metadata_search');
            $this->load->view('pages/metadataview/metadata_form');
        }