Question

I have an app with a table called projects that I'd like to be able to search on. I'd prefer to use an existing method to call the model to get the query results. So my URL format just now is example.com/projects/id/slug and for ALL project just example.com/projects. I'd like to have a search form that passes keywords to the method as a string.

I know CI doesn't allow $_GET by default:

From the CodeIgniter's manual about security:

GET, POST, and COOKIE Data

GET data is simply disallowed by CodeIgniter since the system utilizes URI segments rather than traditional URL query strings (unless you have the query string option enabled in your config file). The global GET array is unset by the Input class during system initialization.

My question is how can I use URI segments in this way with multiple keywords?

Could I do something like search/keyword+secondkeyword+thirdkeyword?

Using a form is there anyway to get keywords from a textbox into the above format?

Thanks,

Billy

Was it helpful?

Solution

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 . '+';
    }
}

OTHER TIPS

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top