Note: I am new to Xoops, and PHP, so …. Keep this in mind.
But this is what I learned in adding a search function to Narga’s myGuestbook module.
1. First thing add this code to the xoops_version.php file in the main directory of the module in question.
For example in the folder called ‘…./modules/myguestbook’ there is a file called xoops_version.php. Open it and add these lines or your version of these lines of code:
// Search $modversion['hasSearch'] = 1; $modversion['search']['file'] = "include/search.inc.php"; $modversion['search']['func'] = "guestbook_search";
2. Second in the include folder (of the module in question) add your php file.
<?php
// This code was borrowed from the search function of NewBB .
// The name of this function is up to you, but it must correspond to the name of this function that you inserted in the xoops_version.php file
// and the parameters "$queryarray, $andor, $limit, $offset, $userid" to this function are, I believe dictated by the core search function
// NOTE: $andor defaults to AND, but can be 'AND', 'OR' or 'exact', corresponding to the three options in the Advanced Search interface. Your search logic should take these three possibilities into account.
function guestbook_search($queryarray, $andor, $limit, $offset, $userid){
// I’m assume this is a global class referring to the database in use. J
global $xoopsDB;
// Start creating a sql string that will be used to retrieve the fields in the table
// that your module is making available to search
// Remember this is a simple module; in the sense is doesn’t have Userids, expiration data for content,
// or Categories, or multiple tables to worry about
$sql = "SELECT id,name,title,message,time FROM ".$xoopsDB->prefix("myguestbook")."";
// because count() returns 1 even if a supplied variable
// is not an array, we must check if $querryarray is really an array
if ( is_array($queryarray) && $count = count($queryarray) ) {
$sql .= " WHERE ((name LIKE '%$queryarray[0]%' OR title LIKE '%$queryarray[0]%' OR message LIKE
%$queryarray[0]%')";
for($i=1;$i<$count;$i++){
$sql .= " $andor ";
$sql .= "(name LIKE '%$queryarray[$i]%' OR title LIKE '%$queryarray[$i]%' OR message LIKE
'%$queryarray[$i]%')";
}
$sql .= ") ";
} // end if
$sql .= "ORDER BY id DESC";
// Because of the way the GuestBook index file displays it's entries I needed to know the total entries.
// Borrowed this code from index.php in the guestbook module directory.
$query = $xoopsDB->query("SELECT COUNT(*) FROM ".$xoopsDB->prefix("myguestbook")." WHERE id>0");
list($numrows) = $xoopsDB->fetchrow($query);
// I assume this is where the sql query gets excuted???
$result = $xoopsDB->query($sql,$limit,$offset);
$ret = array();
$i = 0;
// with the search results, build the links to the hits the search query made
while($myrow = $xoopsDB->fetchArray($result)){
// you can use any gif; I didn't have a specific one and just used one in the images folder.
$ret[$i]['image'] = "images/url.gif";
// since guestbook doesn't have a 'viewentry' function, yet :)
// I simply use the index.php function and calc the offset,
// the offset is starting at the end of the entries..
// if your module has a view entry or veiw article, etc function then simple use that for your link.
$ret[$i]['link'] = "index.php?start=".($numrows-$myrow['id']);
$ret[$i]['title'] = $myrow['name'];
$ret[$i]['time'] = $myrow['time'];
// no user ids in guestbook, so I left this blank.
$ret[$i]['uid'] = "";
$i++;
}
return $ret;
}
// NOTE: as demonstrated above, $ret is a multi-dimensioned array where the first key is an index indicating which result this data pertains to, and then second level keys are: image, link, title, uid and time. These correspond to the individual information components that make up the search result listings. image, uid and time are optional. time should be in a unix timestamp format.
// The search.php file at the root of XOOPS handles search operations. The kernel/module.php file is what actually calls each module's own search function. ?>


![[Main Page]](/modules/mediawiki/images/mediawiki.png)






