繁体   English   中英

搜索过滤器无法与Codeigniter中的分页一起正常使用

[英]Search filter not working properly with Pagination in Codeigniter

我是PHP和CI的新手,正在创建一个搜索过滤器,该过滤器正常工作,直到我移至下一个分页链接为止。 当我在第一个链接上时,数据是根据过滤器/搜索关键字显示的,但是当我移至下一个链接时,所有内容都会熄灭(所有数据在页面上显示)。 我进行了很多搜索,并浏览了许多链接/教程,但没有找到针对此问题的真实/正确/特定的答案。 我在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>

主要问题:

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

我想要的是:

当我浏览分页链接时,它将在“开始搜索”中正常工作。

问题是您在浏览分页链接时未检索到关键字。
分页是常规的<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是否未提供关键字,然后从会话中检索关键字

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM