Here's a simple solution. I'm sure it can be optimized. It will find all instances of a keyword or keywords in a category tree.
Important notes:
- The code below doesn't even think about OR, AND, or NOT queries
- It will match ANY of the keywords entered
Examples:
In search.php, find://-----------------------------------------------------
//--- Show Search Results -----------------------------
//-----------------------------------------------------
add after:// MOD: Search category names for keywords (http://www.4homepages.de/forum/index.php?topic=13675#msg75934)
if (isset($split_words)){
$split_words1 = preg_replace("/^\*/", '', $split_words);
$cat_search_keywords = stripslashes(htmlspecialchars(trim(join("|", $split_words1)))); // clean up user input
$search_cat_result = array(); // initialize array
$resultcat = $site_db->query("SELECT cat_id FROM ".CATEGORIES_TABLE.""); // grab all cats
while ($row = $site_db->fetch_array($resultcat)) { // loop through cats
if ( preg_match( "/($cat_search_keywords)/i", get_category_path_nohtml( $row['cat_id'])) & // find match(es) in cat tree
check_permission("auth_viewcat", $row['cat_id']) ){ // check permission
$search_cat_result[] = get_category_path( $row['cat_id'],1); // add match to array
}
}
$site_db->free_result();
}
$site_template->register_vars( 'cat_wordsearch', (empty($search_cat_result) ? "" : join("\n\n<li>", $search_cat_result)) ); // add result to template
// END MOD: Search category names for keywords
Add in search.html:{if cat_wordsearch}
<h4>category title results:</h4>
<div style="font-size: 90%;height: 100px; border: 1px dashed #47697e; overflow: auto; background-color: #D7DED1">
<p class="text">One or more of your search criteria resulted in a match the following category names.</p>
<ul>{cat_wordsearch}</ul>
</div>
{endif cat_wordsearch}
Corrections and enhancements are welcome.
Note: The get_category_path_nohtml subroutine is found in the
[MOD] Dynamic Page Title for v1.7 & v1.7.1 topic.
Update 2006/08/16: Added auth_viewcat and updated
add match to array line per V@no's suggestion below.
Update 2006/08/20: Mod now checks to ensure that a query was entered before executing.
Update 2006/08/20: Included V@no's modification to register cat_wordsearch when no search keywords present.