Re: Searching

From: Date: Wed, 19 Jul 2000 17:33:38 +0000
Subject: Re: Searching
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-7323@lists.php.net to get a copy of this message
I wrote a function a little while back that will turn a google type search string in the form "+this andthis +thistoo -notthis" into a multiple field search query. It could probably use a little cleanup, but hey it works. function boolean_query ($str, $tablename, $searchfields, $resultfields, $after_where) { // ryan adams 6/6/00 $a_fields = explode (" ", $searchfields); $a = explode (" ", $str); while (list($k, $v) = each ($a)) { if ($v[0] == "-") { $a_query[] = "NOT LIKE \"%".substr ($v, 1)."%\""; } elseif ($v[0] == "+") { $a_query[] = "LIKE \"%".substr ($v, 1)."%\""; } else { // default case $a_query[] = "LIKE \"%$v%\""; } } for ($i=0; $i<@count($a_fields); $i++) { $naq = ""; for ($j=0; $j<count($a_query); $j++) { $naq[]="$a_fields[$i] $a_query[$j]"; } $b_query[] = "(".implode (" AND ", $naq).")"; } $q = implode (" OR ", $b_query); $q1 = "select $resultfields from $tablename where ($q) "; if ($after_where) $q1 .= $after_where; return $q1; } $searchstr = "this +andthis -notthis +yesthis"; $query = boolean_query ($searchstr, "mytable", "field1 field2 field3", "*", "order by anotherfield"); Hope this helps, Ryan On Mon, Jul 17, 2000 at 11:03:17AM -0700, Ed Vul wrote: > I am not quite sure how I should do this: > I need to make a feature that would search several columns in a table for > stuff. > What I'm not sure is, how would I make it work like a normal searchn (using > and, or, etc.), are there some kinds of functions in PHP for this? or do I > have to parse the search string? I'm not quite sure where I would begin. > On top of my php incompetence, I don't know sql very well, so that adds to > my problem. > Any pointers (no pun intended), in the right direction would help. > > Thanks > Ed > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net ---end quoted text---

« previous php.general (#7323) next »