tendaitakas
tendaitakas

Reputation: 358

Codeigniter put variable in front of the page uri segment using the pagination class

I am trying to build a search functionality with pagination using codeigniter. The problem is I can't get to put the search query on the Url in front of the the page number so that I access it using the get method from my controller. How can I get the url to display like below:

http://localhost/project/shop/2/search?search_query=bolts

The 2 being the uri segment that the pagination class uses to identify the page number. Below is part of the pagination class config in my controller:

$search_query=$this->input->get('search_query');

 $config['base_url'] = base_url().'shop/search?search='.$search_query;
 $config['total_rows'] = $this->ProductModel->countallByTerm($search query);
 $config['per_page'] = 2;

 $start = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

 $data['products']=$this->ProductModel->search($search_query, $config['per_page'], $start);
 $this->pagination->initialize($config);

And when I click the 'next page' on my pagination link it is generating the url below:

 http://localhost/project/shop/search?search=bolts/2

This is my html code in the view with the form responsible for submitting the search query:

<form class="" action="<?php echo site_url('shop/search') ?>" method="get">
        <div class="input-field col md10 s10">
          <i class="material-icons prefix">search</i>
          <input type="text" name="search" class="validate" required>
          <label for="search">Search Products</label>
        </div>
        <div class="col md2 s2">
          <input type="submit" class="btn" value="search" name="submit" value="">
        </div>
      </form>

Please help

Upvotes: 1

Views: 871

Answers (1)

Ketan Solanki
Ketan Solanki

Reputation: 697

Change your Form as follows:

<form class="" action="<?php echo site_url('shop/search') ?>" method="get">
        <div class="input-field col md10 s10">
          <i class="material-icons prefix">search</i>
          <input type="text" name="search" id="search" class="validate" required>
          <label for="search">Search Products</label>
        </div>
        <div class="col md2 s2">
          <input type="button" id="SubmitButtonForForm" class="btn" value="search" name="submit" value="">
        </div>
      </form>

And you also have to do the Processing with JQuery:

var url = '<?php echo $_SERVER['REQUEST_URI'];?>';//Made Changes Here.
//It will output the current Page's URl for EG:http://localhost/project/shop/2  
//1 or 2 or... based on your pagination.

$("#SubmitButtonForForm").on("click",function(e){
   //Add your Code for Validations Here and put a condition that if the form is validated then only process the following code.
   var searchString = $("#search").val();
   e.PreventDefault();
   var redirectURl = url + "/" + searchString;
   //Redirect with window location method of javascript.
   window.location = redirectURl;
})

Upvotes: 1

Related Questions