Hacks: Module with Search in HTML

Posted by: ulissesOn 2004/4/12 20:01:26 4355 reads
I am starting in the XOOPS now and therefore I opted to using static pages and deriving data of mysql. First I did not try to use tinycontent, cjaycontent and etc, but it was not what I wanted. Then observing other codes I implemented a module that shows my pages HTML. The problem was when I gave for account that the SEARCH did not function. I looked in several forums. Some said that solution did not exist, others said to use tinycontent and etc. Then, giving for satisfied I did not decide me I myself to implement my SEARCH in HTML. The solution is adopted is simple consists of:



1 - To define tags assistant of the research in the pages htmls. Is they:



<!-- HTMLSEARCH-TITLE="Its Heading HERE" -->
<!-- HTMLSEARCH-LINK="The Link for the page" -->
<!-- HTMLSEARCH-BEGIN-SEARCH -->
The to be looked for text goes HERE
<!-- HTMLSEARCH-END-SEARCH -->



What it must be made is to include these tags in page HTML, can be included how many blocks will be necessary. To observe that it must have Título (HTMLSEARCH-TITLE), Link (HTMLSEARCH-LINK) and only to be definite the beginning (HTMLSEARCH-BEGIN-SEARCH) and the end (HTMLSEARCH-END-SEARCH) of the text where the research will be become fullfilled.

2 - To include in the archive xoops_version.php of the module in question the necessary information for the SEARCH.


// Search
$modversion['hasSearch'] = 1;
$modversion['search']['file'] =
"include/search.inc.php";
$modversion['search']['func'] = "yourmodule_search";


3 - To inside create of the directory include of its module the archive search.inc.php. Inside it must contain the function seumodulo_search(). It is function must return an Array where each line of the Array is an joined item.



$ret[$i]['image'] = $image;
$ret[$i]['link'] = $link;
$ret[$i]['title'] =
$title;
$ret[$i]['uid'] = 1;
$ret[$i]['time'] = filemtime($dir . $file);





The function yourmodule_search() covers archives HTML identifies tags of item 1. E returns the result in the Array. Below it follows my function of search. It verifies all the archives of a directory.


<?php

function produtos_search($queryarray, $andor, $limit, $offset, $userid){

$ret =
array();
$i = 0;
$k = 0;

// dir to search
$dir =
"modules/produtos/templates/";

$image = "images/menu_pe15x17.gif";

if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while
((($file = readdir($dh)) !== false) && ($i < $limit)) {
if (!is_dir($dir . $file)) {
$array_tmp = file($dir .
$file);

$found = false;
$beginsearch = false;
foreach($array_tmp as $v) {


if (substr_count($v,'HTMLSEARCH-TITLE')>=1) {
$begin = strpos($v,
'"')+1;
$end = strrpos($v, '"');

$title =
trim(substr($v,$begin,$end-$begin));
} elseif (substr_count($v,'HTMLSEARCH-LINK')>=1) {

$begin = strpos($v, '"')+1;
$end = strrpos($v, '"');

$link =
trim(substr($v,$begin,$end-$begin));
} elseif (substr_count($v,'HTMLSEARCH-BEGIN-SEARCH')>=1) {

$text2search = "";
$k++;
if ($k > $offset)
$beginsearch =
true;
} elseif ((substr_count($v,'HTMLSEARCH-END-SEARCH')>=1) && $beginsearch) {

$text2search = strtoupper($text2search);
if ( is_array($queryarray) && $count = count($queryarray) ) {

if (strpos($text2search,strtoupper(htmlentities($queryarray[0]))) ||
strpos(strtoupper(htmlentities($title)),strtoupper(htmlentities($queryarray[0]))))
$found = true;

for($j=1;$j<$count;$j++){
if ($andor == 'AND') {
if
(strpos($text2search,strtoupper(htmlentities($queryarray[$j]))) ||
strpos(strtoupper(htmlentities($title)),strtoupper(htmlentities($queryarray[$j]))))
$found = $found && true;

else
$found = $found && false;
} else { // OR

if (strpos($text2search,strtoupper(htmlentities($queryarray[$j]))) ||
strpos(strtoupper(htmlentities($title)),strtoupper(htmlentities($queryarray[$j]))))
$found = $found || true;

else
$found = $found || false;
}

}
}
$beginsearch = false;
}


if ($beginsearch) {
$text2search = $text2search.$v;
}


if ($found) {
$ret[$i]['image'] = $image;
$ret[$i]['link'] = $link;

$ret[$i]['title'] = $title;
$ret[$i]['uid'] = 1;

$ret[$i]['time'] = filemtime($dir . $file);
$i++;
$found = false;

if ($i >= $limit)
break;
}
}
unset($array_tmp);

}
}
closedir($dh);
}
}
return
$ret;
}
?>



If to want to see the Module in action is alone to go in link:http://ciaandarilhos.cordeiropereira.com.br the module of "Produtos" has this implemented code. They see the code of the pages in HTML will have doubts as to place tags. I wait suggestions to optimize the code, as, for example, the code of htmlentities was very bad, to perhaps use regular expressions to find tags and the words to be searched would be better. Well, more ai is to the idea now is alone to share to be able improves it Suggestions will be comings well.

Ulisses