White space in searches

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bk1984
Forum Newbie
Posts: 3
Joined: Fri May 15, 2009 4:21 am

White space in searches

Post by bk1984 »

Hi all,

I am currently trying to fix the search on a website I am trying to develop. What happens is if you search for 'bin' a lot of products come up which contain bin within their product description, this is very annoying as you can imagine because things like post it notes will appear which are totally unrelated.

But when I add spaces into the the search for example (space)bin(space) it will come up with the correct products. I am baffled I have search through the code numerous times and just don't know how to do it. I am a beginner at PHP but hoping to quickly learn so I can get the website running functionally how everyone wants it.

the code is:

Code: Select all

<?php 
 
$app       = $this->globals('khxc_display.app');
$eol       = $this->globals('khxc.eol');
$formid    = $app . '--minisearch';
$strid     = $app . '--prodsearch--string';
$link      = $this->link_namespace($app,'prodsearch',array());
$typeid    = $app . '--prodsearch--type';
$cgi_value = '';
 
$this->xhtml_quickform_header($formid,$app,'prodsearchp',array());
 
print '<p class="hidden"><label for="' . $app . '--minisearch--' . $strid . '"';
print '>Search Term</label></p>' . $eol;
 
print '<p class="inline"><input class="khxc_quickfield" type="text" name="' . $strid;
print '" id="' . $app . '--minisearch--' . $strid . '" value="';
print $cgi_value . '" size="22" maxlength="100" /></p>' . $eol;
 
print '<p class="hidden"><input class="khxc_quickfield" type="text" value="EXACT" name=" ' . $typeid;
print '" id="' . $app . '--minisearch--' . $typeid . '" value="ALL" /></p>' . $eol;
 
$this->xhtml_quickform_footer($formid,'Online Store Search',1);
 
print '<a class="unfancy" href="' . $link . '" title="Advanced Search">Advanced Search</a>' . $eol;
?>
any help would be extremely appreciated
Last edited by Benjamin on Tue Jun 09, 2009 11:40 am, edited 1 time in total.
Reason: Added [code=php] tags.
bk1984
Forum Newbie
Posts: 3
Joined: Fri May 15, 2009 4:21 am

Re: White space in searches

Post by bk1984 »

Sorry didn't even say what I wanted to do ooops, what I want to do is add the spaces automatically so the user doesn't have to. so it would be (space) bin (space) or (space)ball point pen(space)
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: White space in searches

Post by mikemike »

Is this where your script posts to?

You need to find the variable that gets sent to the search page, which will be in $_GET or $_POST form. Normally $_GET if it's a search.

Let's say your search field is called 'search_str', then you simply do:

Code: Select all

$_GET['search_str'] = ' '.$_GET['search_str'].' ';
Put that at the top of the processing page, replacing the variable name for what it needs to be.
bk1984
Forum Newbie
Posts: 3
Joined: Fri May 15, 2009 4:21 am

Re: White space in searches

Post by bk1984 »

Thank you very much, I will give it a go now
Post Reply