search a Tutorial by jacek

Ask about a PHP problem here.
Post Reply
User avatar
i3c
Posts: 14
Joined: Thu Sep 20, 2012 9:56 am
Contact:

search a Tutorial by jacek

Post 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?

[syntax=php]
$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) . "%'";


[/syntax]

thank you.
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: search a Tutorial by jacek

Post 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:
[syntax=sql]SELECT names FROM test LIKE LOWER('%John%')[/syntax]

And you may want to use more variables, to look neater:
[syntax=php]$something = implode("%' OR `post_title` LIKE '%", $keywords);

$title_where = "`post_title` LIKE '%{$something}%'";[/syntax]That's generally how I do it :)

Source: http://forums.cpanel.net/f5/mysql-upper-lowercase-search-problem-49465.html
User avatar
i3c
Posts: 14
Joined: Thu Sep 20, 2012 9:56 am
Contact:

Re: search a Tutorial by jacek

Post by i3c »

hello again,

hmm can you make the example with the code i gived.
because its makes me a little confused :(
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: search a Tutorial by jacek

Post 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...

[syntax=php]$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}%')";[/syntax]
User avatar
i3c
Posts: 14
Joined: Thu Sep 20, 2012 9:56 am
Contact:

Re: search a Tutorial by jacek

Post 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 :(
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: search a Tutorial by jacek

Post by Helx »

Could you please get a post or something and export it for me?
(take out any personal data :P )

So I can test this myself. It could be the RegEx your using.
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: search a Tutorial by jacek

Post 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 :<
User avatar
i3c
Posts: 14
Joined: Thu Sep 20, 2012 9:56 am
Contact:

Re: search a Tutorial by jacek

Post by i3c »

its okay don't need to say sorry :)
im glad you wanted to help :D

thanks so far.
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: search a Tutorial by jacek

Post by Helx »

You could always take a different approach ?

There is a nice little tutorial here: http://www.codeforest.net/simple-search-with-php-jquery-and-mysql
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: search a Tutorial by jacek

Post by jacek »

You could convert the keywords and the stuff in the table both to lowercase before checking.

[syntax=php]$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) . "%'";[/syntax]

note that all those LOWER() calls will not be great for performance so it would be worth looking in to fulltext search.
Image
User avatar
i3c
Posts: 14
Joined: Thu Sep 20, 2012 9:56 am
Contact:

Re: search a Tutorial by jacek

Post by i3c »

thanks jacek it worked :)
Post Reply