搜索过滤器与Codeigniter中的分页无法正常工作

时间:2016-11-20 18:33:53

标签: php search codeigniter-3

我是PHP和C.I的新手,我正在创建一个搜索过滤器,过滤器正常工作,直到我移动到下一个分页链接。当我在第一个链接时,数据显示根据过滤器/搜索关键字,但当我移动到下一个链接时,一切都会消失(所有数据显示在页面上)。 我搜索了很多并经历了许多链接/教程,但没有找到这样的问题的真实/适当/具体的答案。 我在Stack Overflow上找到Link并跟随它,但没有运气。

我的控制器代码:

  public function URecords() {
    $config = array();
    $keyword    = $this->input->post('search');
   $this->session->set_flashdata('search',$keyword);
    $config["base_url"] = base_url() . "master/URecords";
    $config["total_rows"] = $this->bm->record_count($keyword);
    $config['use_page_numbers'] =FALSE;
    $config['cur_tag_open'] = '<a><b class="text-success">';
    $config['cur_tag_close'] = '</b></a>';
    $config["per_page"] =4;
    $config["uri_segment"] = 3;

    $this->pagination->initialize($config);

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data["posts"] = $this->bm->fetch_countries($config["per_page"], $page,$keyword);

    $data["links"] = $this->pagination->create_links();
    $this->load->view('Admin/header');
    $this->load->view('Admin/nav');
    $this->load->view('Admin/sidebar');
    $this->load->view('Admin/userrecord', $data);
    $this->load->view('Admin/footer');
}

我的型号代码:

public function record_count($keyword) {
        $this->db->like('Employee_Name',$keyword);

        $this->db->from('dc_user');
        return $this->db->count_all_results();

       // return $this->db->count_all("dc_user");
    }

    public function fetch_countries($limit, $start,$keyword) {
        $this->db->limit($limit, $start);
        $this->db->like('Employee_Name',$keyword);
        $query = $this->db->get_where("dc_user");
        if(empty($query->result()))
        {
            //echo "No record found in data base";
            $this->session->set_flashdata('msg', '<div class="alert alert-success text-center">No Record Found!</div>');
        }
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

我的观看代码:

   <div class="right_col" role="main">
            <div class="">
                <div class="page-title">

                    <div class="title_right">
                        <div class="col-md-5 col-sm-5 col-xs-12 form-group pull-right top_search">
                            <form action="<?php echo base_url();?>Master/URecords" method="post">
                            <div class="input-group">
                                <input type="text" class="form-control" id="search" name="search" value="<?php if(isset($_SESSION['search'])){echo $_SESSION['search'];}?>" placeholder="Search for...">
                    <span class="input-group-btn">
                        <input class="btn btn-default" type="submit" name="submit" id="submit" value="GO!">

                    </span>
                            </div>
                            </form>
                        </div>
                    </div>
                </div>

                <div class="clearfix"></div>

                <div class="row">



                    <div class="col-md-12 col-sm-12 col-xs-12">
                        <div class="x_panel">
                            <div class="x_title">
                                <h2>All User's Record</h2>
                                <ul class="nav navbar-right panel_toolbox">
                                    <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                                    </li>
                                    <li class="dropdown">
                                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
                                        <ul class="dropdown-menu" role="menu">
                                            <li><a href="#">Settings 1</a>
                                            </li>
                                            <li><a href="#">Settings 2</a>
                                            </li>
                                        </ul>
                                    </li>
                                    <li><a class="close-link"><i class="fa fa-close"></i></a>
                                    </li>
                                </ul>
                                <div class="clearfix"></div>
                            </div>


                            <div class="x_content">
                                <div class="table-responsive">
                                    <?php if(isset($_SESSION['msg'])){?>
                                    <span class="text-info col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
                           <?php echo $this->session->flashdata('msg'); ?>
                          </span>
                                  <?php  } else {?>
                                    <table class="table table-striped jambo_table bulk_action">
                                        <thead>
                                        <tr class="headings">

                                            <th class="column-title">Employee Name </th>
                                            <th class="column-title">Email </th>
                                            <th class="column-title">Contact # </th>
                                            <th class="column-title">Date Of Birth </th>
                                            <th class="column-title">Designation </th>
                                            <th class="column-title">Profile Picture </th>


                                        </tr>
                                        </thead>

                                        <tbody>
                                        <?php foreach($posts as $post) { ?>
                                            <tr class="even pointer">
                                                <td class=" "><?php echo $post->Employee_Name; ?></td>
                                                <td class=" "><span class="text-info"><?php echo $post->Email; ?></span></td>
                                                <td class=" "><?php echo $post->Contact; ?></td>
                                                <td class=" "><?php echo $post->DOB; ?></td>
                                                <td class=" "><?php echo $post->Designation; ?></td>
                                                <td class=" "><img src="<?php echo base_url();?>profileimages/<?php echo $post->Profile_Image; ?>" alt="..." class="img-square profile_img" style="width: 200px !important; height: 100px !important;"> </td>
                                                </td>
                                            </tr>
                                        <?php } ?>




                                        </tbody>
                                    </table>

                                    <div class="text-center"><nav aria-label="Page navigation">
                                            <ul class="pagination">

                                                <li><?php echo $links; ?></li>

                                            </ul>
                                        </nav></div>

<?php }?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

主要问题:

问题是:当我在搜索字段中输入任何关键字时,它将在开始时显示正确的结果,但是当我移动到其他分页链接时,我将丢失搜索结果。

我想要的:

当我浏览分页链接时,它将在搜索开始时起作用。

1 个答案:

答案 0 :(得分:1)

问题是您在浏览分页链接时没有检索关键字 分页是常规<a>所以它不发送任何POST数据

替换此行:

$keyword    = $this->input->post('search');
$this->session->set_flashdata('search',$keyword);

用这个:

$keyword    = $this->input->post('search');
if ($keyword === null) $keyword = $this->session->userdata('search');
else $this->session->set_userdata('search',$keyword);

此代码将关键字保存到会话中,并检查POST是否未提供关键字,然后从会话中检索关键字