Implementing a search engine
In order to implement a search engine, we need to make provision for multiple columns to be included in the search. In addition, it's important to recognize that the search item might be found in the middle of the field, and that very rarely will users provide enough information for an exact match. Accordingly, we will rely heavily on the SQL LIKE %value%
clause.
How to do it...
- First, we define a basic class to hold search criteria. The object contains three properties: the key, which ultimately represents a database column; the operator (
LIKE
,<
,>
, and so on); and optionally an item. The reason why an item is optional is that some operators, such asIS NOT NULL
, do not require specific data:namespace Application\Database\Search; class Criteria { public $key; public $item; public $operator; public function __construct($key, $operator, $item = NULL) { $this->key = $key; $this->operator = $operator; $this->item = $item...