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) Controller: Code: function index() 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){ 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) If you will return $val variable, then you can simply in your controller do this: Code: $val_array = $this->some_sort_model->search($key); 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){ 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? |