note 40403 deleted from function.implode by nlopess

From: Date: Wed, 03 Mar 2004 17:44:11 +0000
Subject: note 40403 deleted from function.implode by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-66194@lists.php.net to get a copy of this message
Note Submitter: daevid@daevid.com ---- I wanted a way to search BOTH the Title (varchar) and Body(text) fields of a database, but couldn't figure out the proper way to form the SELECT statement... given an array named $words for each of the keywords you wish to find, then just do this: $SQL = "SELECT * FROM foo WHERE 1"; if (count($words) > 0) { $SQL .= " AND ( "; $tmp = array(); foreach($words as $key => $word) $tmp[] = "Title like '%".$word."%'"; $SQL .= "(".implode(" AND ",$tmp).") "; $SQL .= " OR "; $tmp = array(); foreach($words as $key => $word) $tmp[] = "Body like '%".$word."%'"; $SQL .= "(".implode(" AND ",$tmp).") "; $SQL .= ") "; } As an added bonus, if you're really cool, then you can do something like this to highlight the words after you pull them back out of the db (set up your CSS appropriately): if (count($words) > 0) foreach($words as $key => $word) $row['Title'] = eregi_replace ($word, "<FONT CLASS='foundWord'>\\0</FONT>", $row['Title']);

« previous php.notes (#66194) next »