user3721091
user3721091

Reputation:

Search filter not working properly with Pagination in Codeigniter

I am new to PHP and also C.I, I am creating a Search Filter, The filter is working properly until i moved to the next Pagination link. When i am on First link the data is showing according to the filter/search keyword, but as i move to the next link everything goes off (All the data shows on page). I searched a lot and go through many links/tutorials but did not find authentic/proper/specific answer for such issue. I find a Link here on Stack Overflow and follow it, but got no LUCK.

My Controller Code:

  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');
}

My Model Code:

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;
    }

My View Code:

   <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>

Main Issue:

The Issue is: When i enter any keyword in search field then it will show the correct result on start but as i move to the other pagination links then i lose the search result.

What I Want:

When I Go through Pagination links then it will works as it work on Start of Search.

Upvotes: 0

Views: 2612

Answers (1)

Atmahadli
Atmahadli

Reputation: 687

the problem is you did not retrieve the keyword when navigate through pagination links.
pagination is a regular <a> so it is not sending any POST data

replace this line:

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

with this:

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

this code save the keyword into session, and check if the keyword is not provided by POST then retrieve the keyword from session

Upvotes: 1

Related Questions