brettski Posted October 11, 2011 Share Posted October 11, 2011 Hi All, Just a couple of things with my site search, I am having trouble getting the search results to pickup any and all keywords. For example, if keywords for a search result are listed in the keywords column of my db as 'one, two, three' a search query for 'one two' is fine but a search for 'one three' will not display the search result. Instead of treating the keywords seperately it is treating all keywords as a whole phrase. How can I make the search results pickup any keyword regardless of order. Secondly I'm having trouble getting the search results to display by keyword relevance, any help is greatly appreciated. Thanks. function search() { parent::Controller(); $this->load->model('Templating'); $this->load->model('Company'); } function index() { $this->load->view('search'); } function search_redirect() { if ($_POST['keyword'] == '') $_POST['keyword'] = 'Keyword'; redirect('search/showlist/'. urlencode(underscore($_POST['location'])).'/'. urlencode(underscore($_POST['category'])).'/'. urlencode(underscore($_POST['keyword']))); } function showlist($location = 'any', $category = 'any', $keyword = 'keyword', $offset = '0') { /* get results */ $data['companies'] = $this->Company->search($keyword, $category, $location, $offset, $name); /* paginate results */ $row = $data['companies']->row(); if($keyword == 'keyword' and $category=='any' ) { $data['categoryList'] = $this->buildCategoryList($location); } elseif(isset($row->categoryId)) { $categoryId = $row->categoryId; $data['linkList'] = $this->buildRefineList($categoryId); } $this->load->library('pagination'); $config['base_url'] = site_url().'search/showlist/'.$location.'/'.$category.'/'.$keyword.'/'; $config['total_rows'] = $this->Company->total_companies; $config['per_page'] = $this->Company->per_page; $config['num_links'] = 3; $config['uri_segment'] = $this->uri->total_segments(); $this->pagination->initialize($config); $data['pagination'] = $this->pagination->create_links(); $data['logged_in'] = $this->session->userdata('logged_in'); $data['company_id'] = $this->session->userdata('company_id'); $data['search_category'] = $category; $data['search_location'] = $location; if ($this->session->userdata('admin') != ''){ $data['admin'] = $this->session->userdata('admin'); } /* initialise template settings */ $center = 'center/list_companies'; $this->load->vars($data); $this->Templating->show($center); } Quote Link to comment https://forums.phpfreaks.com/topic/248882-search-query-keyword-issue/ Share on other sites More sharing options...
WebStyles Posted October 11, 2011 Share Posted October 11, 2011 I confess I didn't look at your code (I'm at work and don't have much time right now, so I'm only guessing, maybe you're already doing this, in which case I apologize.) but if you have a field with 'one two three' and you search for 'one three', you're gonna need to use LIKE and the wildcard % so you could basically replace all spaces in the keyword with % (and also add it to beginning and end for other situations to work too)... so: $keywords = "%".str_replace(" ","%",trim($_POST['keywords']))."%"; then use something like mysql_query("select filed1,field2 from table where keywords like '$keywords'"); hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/248882-search-query-keyword-issue/#findComment-1278121 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.