CodeIgniter Forums

Full Version: PHP search with spaces
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]Kraig[/eluser]
Currently I have a a search that searches through uploaded file names. The search works great if you only have one word, but when I start putting spaces it doesn't do what I want it to. I'm thinking in order to search for each word I will need to explode the string and search the array. Any other good ideas out there?
Here's my model:
Code:
public function search($key)
{
  $query = $this->db->query('SELECT * FROM upload WHERE name LIKE "%'.$key.'%"');
  $this->num_rows = $query->num_rows();
  $val = $query->result_array();
  
  return $query;
}

Controller:
Code:
function index()
{
  $name= $_POST['keywords'];
  $val = "";
  $search = $this->upload_model->search($name);
  
  if(strlen($name) < 4) {
   $this->error = "Minimum search length is 3.";
  }else {
   if($search->num_rows() > 0) {
    $val = $search->result_array();
   }else {
    $this->error = "Sorry, no matches were found.";
   }
  }
  
  $data['search'] = $val;
  $data['error'] = $this->error;
  
  $data['main_content'] = 'search';
  $this->load->view('includes/template2', $data);

}

El Forum

[eluser]DarkManX[/eluser]
Well, what to you expect this query to find? If you enter like "car tree" for keys to search, you will the data having a name like "forum ball car tree boy". There might be a more elegant way to solve this, but you can split your keys and create for each of the keyword "or_like()" in the active record.
That would work for sure.

El Forum

[eluser]Kraig[/eluser]
If I split my keys would I have to run that query "x" amount of time? Or is there a way to use for each for that? Just not sure how it should look. One word works fine though. If I try something like "good beer"

DB:
good beer
good dang beer
great beer
some good freaking beer

Only the one that matches it exactly will be a result. I want to get all of them, because they at least have one of the words in them from the keyword.

El Forum

[eluser]DarkManX[/eluser]
Code:
foreach($keys as $key){
   $this->db->or_like($key);
}

That way there will be just one mysql query.

El Forum

[eluser]l1v1[/eluser]
Code:
public function search($key)
{
  $query = $this->db->query('SELECT * FROM upload WHERE name LIKE "%'.$key.'%"');
  $this->num_rows = $query->num_rows();
  $val = $query->result_array();
  
  return $query;
}
Why are you returning $query variable? I think you need to return the $val variable.
If you will return $val variable, then you can simply in your controller do this:
Code:
$val_array = $this->some_sort_model->search($key);
for($i=0;$i<count($val_array);$i++){
echo $val_array[$i];
}

El Forum

[eluser]l1v1[/eluser]
You have to explode your $key variable with ' ' and run the query "x" times, like you said. I don't think if there is another way to do that.
I didn't test this code, so I don't know if it works, but try something like this:

Code:
public function search($key){
$query_array = array();
$keys = explode(' ',$key);
for($i=0;$i<count($keys);$i++{
  $this->db->like('name',$keys[$i]);
  $query = $this->db->from('upload');
   if($query->num_rows()>0){
    array_push($query_array,$query->result_array());
   }
   else{
    return FALSE;
   }
}
}

El Forum

[eluser]Kraig[/eluser]
So I would return array_push($query_array,$query->result_array());

El Forum

[eluser]Mirge[/eluser]
Don't run several queries. Do as DarkManX said.

El Forum

[eluser]l1v1[/eluser]
[quote author="Mirge" date="1345814736"]Don't run several queries. Do as DarkManX said.[/quote]

Why? Just wondering.

El Forum

[eluser]CroNiX[/eluser]
Turn it around. Why would you use many queries when you can use just one?
Pages: 1 2