代码点火器2中的寻呼链接的结果为空


Pagination links in codeigniter 2 went to empty result

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

这是代码:

控制器功能

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>

分页类创建的链接不会包含"search_by"answers"keyword"条件,它们肯定不会出现在post变量中,因为当您单击页面链接时,您正在执行HTTP GET。

您需要考虑将条件存储在用户会话之类的地方,以便对每个页面请求重用,或者使用CodeIgniter 3中的分页库,该库支持添加项目(如两个搜索项)作为查询字符串参数,然后您也可以检查这些参数。

关于CodeIgniter3分页类的详细信息可以在这里找到。

我自己没有尝试过,但你也应该能够修改分页链接的URL,这样它们就会显示:

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

然后,您可以读取各个URL段来检索搜索词。