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; ...
Get PHP 7 Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.