Page 1 of 1
search a Tutorial by jacek
Posted: Fri Sep 21, 2012 9:13 am
by i3c
hello again,
so can anybody explain how to search the title or body on every lower or uppercase.
because if i add a lowercase letter into the search box he can't find it
when i add a uppercase in the search box then he find it.
is there anyway to do that?
$keywords = preg_split('#\s+#', mysql_real_escape_string($term));
$title_where = "`post_title` LIKE '%" . implode("%' OR `post_title` LIKE '%", $keywords) . "%'";
$body_where = "`post_body` LIKE '%" . implode("%' OR `post_body` LIKE '%", $keywords) . "%'";
thank you.
Re: search a Tutorial by jacek
Posted: Fri Sep 21, 2012 10:32 am
by Helx
Hi there
Somebody smarter than me please correct me if I'm wrong, but BLOB is case sensitive (if that's what your using)
Depending on whats there, you may want to convert input to lower case with the MySQL 'lower' function.
An example:
SELECT names FROM test LIKE LOWER('%John%')
And you may want to use more variables, to look neater:
$something = implode("%' OR `post_title` LIKE '%", $keywords);
$title_where = "`post_title` LIKE '%{$something}%'";
That's generally how I do it
Source: http://forums.cpanel.net/f5/mysql-upper ... 49465.html
Re: search a Tutorial by jacek
Posted: Fri Sep 21, 2012 10:40 am
by i3c
hello again,
hmm can you make the example with the code i gived.
because its makes me a little confused
Re: search a Tutorial by jacek
Posted: Fri Sep 21, 2012 10:52 am
by Helx
i3c wrote:hello again,
hmm can you make the example with the code i gived.
because its makes me a little confused
Sure!
Just know that I have not tested this...
$keywords = preg_split('#\s+#', mysql_real_escape_string($term));
$implode_title = implode("%' OR `post_title` LIKE '%", $keywords);
$implode_body = implode("%' OR `post_body` LIKE '%", $keywords);
$title_where = "`post_title` LIKE LOWER('%{$implode_title}%')";
$body_where = "`post_body` LIKE LOWER('%{$implode_body}%')";
Re: search a Tutorial by jacek
Posted: Fri Sep 21, 2012 10:59 am
by i3c
thanks for the fast answer.
okay works but seems it only search on the first word off the title
so it don't search anymore on the complete string
Re: search a Tutorial by jacek
Posted: Fri Sep 21, 2012 11:06 am
by Helx
Could you please get a post or something and export it for me?
(take out any personal data
)
So I can test this myself. It could be the RegEx your using.
Re: search a Tutorial by jacek
Posted: Fri Sep 21, 2012 11:41 am
by Helx
Okay, I noticed that entering one word into the search in upper or lower case will not matter.
However, when you enter 2 words, it will say that no results were found. (and it won't search the body)
I noticed that the search terms are glued together with "+" instead of standard spaces. This could only bring me to
your regex: "#\s+#"
This is where I get off. I have no knowledge of RegEx (or search in general).
Your probably better off waiting for somebody else to pick up this thread :3
Sorry :<
Re: search a Tutorial by jacek
Posted: Fri Sep 21, 2012 11:53 am
by i3c
its okay don't need to say sorry
im glad you wanted to help
thanks so far.
Re: search a Tutorial by jacek
Posted: Sat Sep 22, 2012 9:29 pm
by Helx
You could always take a different approach ?
There is a nice little tutorial here:
http://www.codeforest.net/simple-search ... -and-mysql
Re: search a Tutorial by jacek
Posted: Sat Sep 22, 2012 11:07 pm
by jacek
You could convert the keywords and the stuff in the table both to lowercase before checking.
$keywords = preg_split('#\s+#', mysql_real_escape_string(strtolower($term)));
$title_where = "LOWER(`post_title`) LIKE '%" . implode("%' OR LOWER(`post_title`) LIKE '%", $keywords) . "%'";
$body_where = "LOWER(`post_body`) LIKE '%" . implode("%' OR LOWER(`post_body`) LIKE '%", $keywords) . "%'";
note that all those LOWER() calls will not be great for performance so it would be worth looking in to fulltext search.
Re: search a Tutorial by jacek
Posted: Sat Sep 22, 2012 11:42 pm
by i3c
thanks jacek it worked