note 40403 added to function.implode

From: Date: Wed, 03 Mar 2004 01:24:30 +0000
Subject: note 40403 added to function.implode
Groups: php.notes 
Request: Send a blank email to php-notes+get-66135@lists.php.net to get a copy of this message
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']); ---- Manual Page -- http://www.php.net/manual/en/function.implode.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40403 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40403&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40403&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#66135) next »