For an all-words Boolean search for
???beautiful flower???, the search string we send to the database is "+beautiful +flower". For any-words
searches, we simply send "beautiful flower". The following code creates and populates the $search_
string variable, which contains exactly the string that MySQL needs to search for:
// Build $search_string from accepted words list
$search_string = '';
CHAPTER 8 ?– SEARCHING THE CATALOG 236
// If $allWords is 'on' then we append a ' +' to each word
if (strcmp($allWords, "on") == 0)
$search_string = implode(" +", $search_result['accepted_words']);
else
$search_string = implode(" ", $search_result['accepted_words']);
The following code in Search() is the typical code that requests a page of products, and you??™re familiar with it
from the chapters where you created the product catalog. The function ends by returning $search_result,
which contains the search results, and the lists of accepted and ignored words, which represents all the data you
want to show your visitor when a search is performed:
// Execute the query
$search_result['products'] = DatabaseHandler::GetAll($sql, $params);
// Return the results
return $search_result;
}
Implementing the Presentation Tier
The catalog-searching feature has two separate interface elements that you need to implement:
??? A componentized template named search_box, whose role is to provide the means to
enter the search string for the visitor (refer to Figure 8-1)
??? A componentized template named search_results, which displays the products
matching the search criteria (refer to Figure 8-2)
You??™ll create the two componentized templates in two separate exercises.
Pages:
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350