Search operators

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
chris7889
Forum Newbie
Posts: 4
Joined: Thu Feb 25, 2010 9:12 am

Search operators

Post by chris7889 »

Post from a newB. I have a peice of code which as I understand provides the search operators. Currently it only allows for searching one word. I would like for the search to insert % wherever there is a space and at the begining and end of the search.
e.g. 'please search this' would search for '%please%search%this%' .

This may not be enough code to work from or im heading down the wrong route. If so then appologies. Any help appreciated.

Code: Select all

} else {
                            $val = CRM_Utils_Type::escape( strtolower(trim($value)), 'String' );
                            if ( $wildcard ) {
                                $val = strtolower( addslashes( $val ) );
                                $val = "%$val%";
                                $op  = 'LIKE';
                            }
                            $this->_where[$grouping][] = "{$sql} {$op} '{$val}'";
                            $this->_qill[$grouping][]  = "$field[label] $op $qillValue";
                        }
                    } 
                    continue;
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Search operators

Post by AbraCadaver »

You should use the appropriate *_real_escape_string() or whatever instead of addslashes unless that is what CRM_Utils_Type::escape() is doing, then you can delete the addslashes.

Code: Select all

$val = strtolower( addslashes( $val ) );
 
// works but would replace sequential spaces with multiple %
$val = str_replace( ' ', '%', $val );
 
// replaces sequential spaces with only one %
$val = preg_replace('/[ ]+/', '%', $val);
 
$val = "%$val%";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
chris7889
Forum Newbie
Posts: 4
Joined: Thu Feb 25, 2010 9:12 am

Re: Search operators

Post by chris7889 »

Brilliant. That worked a treat. Thank you AbraCadaver :D
chris7889
Forum Newbie
Posts: 4
Joined: Thu Feb 25, 2010 9:12 am

Re: Search operators

Post by chris7889 »

Thank you very much for your help.

At the moment the search for 'please search this' would search for '%please%search%this%' . Meaning the result has to have all three. Is there a change that can be made so it searches for. %please% or %search% or %this% ?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Search operators

Post by AbraCadaver »

chris7889 wrote:Thank you very much for your help.

At the moment the search for 'please search this' would search for '%please%search%this%' . Meaning the result has to have all three. Is there a change that can be made so it searches for. %please% or %search% or %this% ?
Something similar to this:

Code: Select all

$condition = "WHERE `field_name` LIKE '%" . implode("%' OR `field_name` LIKE '%", explode(" ", $val)) . "%'";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply