CodeIgniter Forums
PHP search with spaces - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: PHP search with spaces (/showthread.php?tid=54108)

Pages: 1 2


PHP search with spaces - El Forum - 08-22-2012

[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);

}



PHP search with spaces - El Forum - 08-22-2012

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


PHP search with spaces - El Forum - 08-22-2012

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


PHP search with spaces - El Forum - 08-22-2012

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

That way there will be just one mysql query.


PHP search with spaces - El Forum - 08-23-2012

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



PHP search with spaces - El Forum - 08-23-2012

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



PHP search with spaces - El Forum - 08-24-2012

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


PHP search with spaces - El Forum - 08-24-2012

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


PHP search with spaces - El Forum - 08-24-2012

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

Why? Just wondering.


PHP search with spaces - El Forum - 08-24-2012

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