Codeigniter: implementing search filters without using $_GET Codeigniter: implementing search filters without using $_GET codeigniter codeigniter

Codeigniter: implementing search filters without using $_GET


If you wanted to do it like that...

You could just do something like this, assuming there are multiple post inputs and they are only for the search, it could look something like this

function search(){  $url = $this->uri->segment(2);  if(empty($url)){      if((isset($_POST) && (!empty($_POST))      {         $search_seg = '';         foreach($_POST as $k => $v){             // I always make sure to use the CI post w/              // TRUE set in the second param so as to XSS filter the user input             $var = $this->input->post($k, TRUE);             // Just incase the user input had a plus in it?             $var = str_replace('+', '%20', $var)             // Concatenate the search string             $search_seg .= $var . '+';         }         // Make the url, use substr() to strip the last + off the end          $search_seg = 'search/' . substr($search_seg, 0, -1);         /* Make sure CI's URL helper is enabled            $this->load->helper('url'); if it hasn't been loaded            This will give the illusion that the form post went to this URL,            thus doing what you're looking for... */         redirect($search_seg, 'location');      }else{         // There was no post, do whatever      }  }else{      $search_arr = explode('+', $url);  }

The above should do pretty much exactly what you described, although there are ways to recreate the $_GET array and still use them with the CI style URL's, although that is a little more complicated

ADDITIONAL INFO:

if there is only one search input field and, say, the terms are separated by spaces, then you may want to do it like this (with maybe some regex filtering)... replace the foreach($_POST as $k => $v)... loop with this:

$keywordinput = $this->input->post('keywords', TRUE);$keywordinput = trim($keywordinput);$keywords = explode(' ', $keywordinput);foreach($keywords as $word){    if(!empty($word)){       $word = str_replace('+', '%20', $var)       $search_seg .= $word . '+';    }}


using $_POST might be a better option for complex queries, but this may work, but requires the URI to have a specific order of parameters (there may be better ways)

/*controller/method/params = search/do_search/Ross/Red/Male */function do_search($name, $colour, $gender){    $this->db->or_where('name', $name);    $this->db->or_where('colour', $colour);      $this->db->or_where('gender', $gender);    // etc}

not very extendible or flexible, but for simple searches it might be enough.


if you want your url to be like this -

search/keyword+secondkeyword+thirdkeyword? 

and also want it to be bookmarkable then using javascript is only option left i guess.

Using javascript you can set search parameters in url after # so the final url will be something like this -

search#keyword+secondkeyword+thirdkeyword

If you use this approach that means that you will load your search result through ajax ie each time when a url is encountered then you will get search keywords after hash from url by javascript. load results through ajax and display the results. If the search criteria is changed then every time you need to append the new keyword to the url after hash using javascript.

Basic javascript code to get the parameters in url after # is as below -

var parameters = window.location.hash;