RunCms имеет способность искать поперек таблиц модуля, таким образом Вы можете
найти всю справочную информацию на специфическую тему. Это
действительно ценно, поскольку это - единственный способ связать вывод
запроса на сайте. Если у Вас на сайте много тем, тогда, я предлагаю,
что (a) Вы удостоверяетесь, что поиск работает, и (b) Вы на практике добавляете поиск ключевых слов в форме так, чтобы поиск нашел их. (я не забываю никогда это делать! Я добавляю <hr/>
keywords: word1, word 2 в конце статьи или FAQ или в другом месте).
Мы
нуждаемся в новом подкаталоге, чтобы добавить функцию поиска, поэтому
создайте mymodule/include, и скопируйте обычный index.html файл сюда.
Примечание
: подкаталог 'include' обычно используется для файлов, которые не будут
выполняться сами, но подключаются при выполнении других файлов.
Полезное место для того, чтобы помещать функции, которые Вы хотите
вызывать из различных частей вашего модуля.
Теперь мы только
собираемся создавать один файл с нашей функцией поиска. Мы назовем его
mymodule_search.php. Здесь - прокомментированный код, который мы хотим
ввести
<?php
/* this is the standard form of search function. The variables are:
$queryarray: one or more words entered in the search box;
$andor: the option from the advanced search drop-down;
$limit: the number of results returned before the 'show all results' message appears. Initial value is 5;
$offset: the place to start from when paging through a long list of results;
$userid: the id of the user calling the search. */
function mymodule_search($queryarray, $andor, $limit, $offset, $userid) {
global $db;
$return = array();
// Set up the search query.
$table = $db->prefix("myquotes");
$query = "SELECT id, quotes FROM $table";
/* because count() returns 1 even if a supplied variable is not an array, we must check if $querryarray is really an array */
$count = count($queryarray);
if ( $count > 0 && is_array($queryarray) ) {
/* Note: in our example we're only searching the 'quote' field. If you have more than one field to search then the following entries need to be more complex.*/
$query .= " AND ((quote LIKE '%$queryarray[0]%')";
for ($i=1; $i<$count; $i++) {
$query .= " $andor ";
$query .= "(quote LIKE '%$queryarray[$i]%')";
}
$query .= ") ";
}
$result = $db->query($query, $limit, $offset);
/* Write the search results to the return array. Note that this example includes a couple of things that we haven't defined. A module icon; and a 'showquote' function. It makes more sense if this is a function that displays a page rather than a block but the principles are the same as we described above.*/
/* The $return array can use the following variables (most can be omitted with varying degrees of usefulness):
$return[$i]['image'] : the icon shown next to the search result
$return[$i]['link'] : where to go when the result is clicked
$return[$i]['title'] : the text in the link
$return[$i]['content'] : a short piece of content
$return[$i]['uid'] : the user id associated with the item
$return[$i]['time'] : the time it was posted */
$i = 0;
while ($myrow = $db->fetch_array($result)){
$return[$i]['image'] = "images/quote.gif";
$return[$i]['link'] = "mymodule.php?op=showquote&id=".$myrow['id']."";
$return[$i]['content'] = $myrow['quote'];
$i++;
}
return $return;
}
?>
Но RunCms не знает, где находится функция поиска, поэтому заходим в xoops_version.php и указываем:
// Search
// There is a search function for this module
// set to zero to turn search off temporarily
$modversion['hasSearch'] = 1;
// Here's where to look for the search function
$modversion['search']['file'] = "include/mymodule_search.php";
// and here's the function to call
$modversion['search']['func'] = "mymodule_search";
Код
для файлов этого модуля указан ниже, но вероятно даже более бесполезен
чем другие файлы, поскольку это не будет работать правильно без
указанных showquote функция. Но зато достаточно хорошо показаны
внутренние принципы