Jump to content

search query & keyword issue


brettski

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/248882-search-query-keyword-issue/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.