codeigniter 2中的分页链接转为空结果

时间:2015-03-04 02:37:22

标签: php pagination codeigniter-2

我试图在搜索结果中创建分页。我得到了整个搜索结果和正确的分页链接数,但是当试图转到下一页时,搜索结果变为空白。第一页应该没问题。但它没有显示任何错误。

以下是代码:

控制器功能

public function staffsearch() {
        if ($this->session->userdata('logged_in') == FALSE) {
            $this->index();
        } else {
            $data['action'] = site_url('user/staffsearch/');
            $search_by = $this->input->post('search_by');
            $keyword = $this->input->post('keyword');

            if (!empty($search_by) && !empty($keyword)) {
                $uri_segment = 3;
                $offset = $this->uri->segment($uri_segment);
                $staff_details = $this->User_Model->get_staff_search_result($search_by, $keyword, $this->limit, $offset)->result();
                $query = $this->db->query("SELECT * FROM `staff` WHERE `" . $search_by . "` LIKE '%" . $keyword . "%'");
                $count = $query->num_rows();

                $this->load->library('pagination');
                $config['base_url'] = site_url('user/staffsearch/');
                $config['total_rows'] = $count;
                $config['per_page'] = $this->limit;
                $config['uri_segment'] = $uri_segment;
                $this->pagination->initialize($config);
                $data['pagination'] = $this->pagination->create_links();


                //$staff_details = $this->User_Model->get_staff_search_result($search_by, $keyword)->result();
                $this->load->library('table');
                $this->table->set_empty(' ');
                $this->table->set_heading('S/No', 'Image', 'Name', 'Office', 'Phone');
                $i = 0;
                foreach ($staff_details as $staff) {
                    if ($staff->Office) {
                        $id = $staff->Office;
                        $staff_office = $this->User_Model->get_office_by_id($id)->row();
                        $office = $staff_office->building . ' ' . $staff_office->level . '-' . $staff_office->unit;
                    } else {
                        $office = '';
                    }
                    if ($staff->Photo_small == '') {
                        $pic = 'unavailable.jpg';
                    } else {
                        $pic = $staff->Photo_small;
                    }
                    $this->table->add_row(++$i, '<image src="' . base_url() . 'people/' . $pic . '" width="50" height="50">', anchor('user/staffdetails/' . $staff->people_id, $staff->Name), $office, $staff->Phone);
                }
                $data['title'] = 'Search Result';
                $data['table'] = $this->table->generate();
            }
            $this->load->view('search_staff', $data);
        }
    }

模特功能:

function get_staff_search_result($fiels, $key,$limit = 10, $offset = 0) {
        if ($fiels == 'Name') {
            $this->db->like('Name', $key);
        } elseif ($fiels == 'Staffnumber') {
            $this->db->like('Name', $key);
        } else {
            $this->db->like('Appointment', $key);
        }
        $this->db->order_by('Name', 'asc');
        return $this->db->get($this->tbl_staff,$limit, $offset);
    }

查看:

<div>
    <h2><?php if (isset($title)) { echo $title;} ?></h2>
    <?php if (isset($table)) { echo $table; } ?>
    <?php if (isset($pagination)) { echo $pagination;} ?>
</div>

1 个答案:

答案 0 :(得分:0)

分页类创建的链接不会包含&#39; search_by&#39;和&#39;关键字&#39;标准,当你点击正在进行HTTP GET的页面链接时,它们肯定不会出现在后期变量中。

您需要考虑将条件存储在用户会话等某个位置,以便可以为每个页面请求重复使用,或者使用CodeIgniter 3中支持添加项目的分页库(例如您的两个搜索术语)作为查询字符串参数,您也可以检查它们。

可以找到有关CodeIgniter 3分页类的详细信息here

我自己没试过,但您也应该能够修改分页链接的网址,以便他们阅读:

site_url('user/staffsearch/') . $search_by .'/' . $keyword;

然后,您可以阅读各个网址段以检索搜索字词。