Prev | Current Page 334 | Next

Emilian Balanescu and Cristian Darie

"Beginning PHP and MySQL E-Commerce: From Novice to Professional, Second Edition"

; ';
/* On the first call to strtok you supply the whole
search string and the list of delimiters.
It returns the first word of the string */
$word = strtok($searchString, $delimiters);
// Parse the string word by word until there are no more words
CHAPTER 8 ?–  SEARCHING THE CATALOG 233
while ($word)
{
// Short words are added to the ignored_words list from $search_result
if (strlen($word) < FT_MIN_WORD_LEN)
$search_result['ignored_words'][] = $word;
else
$search_result['accepted_words'][] = $word;
// Get the next word of the search string
$word = strtok($delimiters);
}
// If there aren't any accepted words return the $search_result
if (count($search_result['accepted_words']) == 0)
return $search_result;
// Build $search_string from accepted words list
$search_string = '';
// 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']);
// Count the number of search results
$sql = 'CALL catalog_count_search_result(:search_string, :all_words)';
$params = array(':search_string' => $search_string,
':all_words' => $allWords);
// Calculate the number of pages required to display the products
$rHowManyPages = Catalog::HowManyPages($sql, $params);
// Calculate the start item
$start_item = ($pageNo - 1) * PRODUCTS_PER_PAGE;
// Retrieve the list of matching products
$sql = 'CALL catalog_search(:search_string, :all_words,
:short_product_description_length,
:products_per_page, :start_item)';
// Build the parameters array
$params = array (':search_string' => $search_string,
':all_words' => $allWords,
':short_product_description_length' =>
SHORT_PRODUCT_DESCRIPTION_LENGTH,
':products_per_page' => PRODUCTS_PER_PAGE,
':start_item' => $start_item);
CHAPTER 8 ?–  SEARCHING THE CATALOG 234
// Execute the query
$search_result['products'] = DatabaseHandler::GetAll($sql, $params);
// Return the results
return $search_result;
}
How It Works: The Business Tier Search Method
The Search() method removes words that are shorter than the length specified by ft_min_word_len.


Pages:
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346