User Login    
 + Register
  • Main navigation
Login
Username:

Password:

Remember me



Lost Password?

Register now!
Who's Online
112 user(s) are online (8 user(s) are browsing XoopsWiki)

Members: 2
Guests: 110

ybk78, crookedbranc, more...
[Main Page]

Dev:search function

From XOOPS Project

Main Page | Recent changes | Edit this page | Page history | Switch to MediaWiki mode

Printable version | Disclaimers | Privacy policy
Category: Development

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. ?>

Retrieved from "http://www.xoops.org/modules/mediawiki/index.php/Dev:search_function"

This page has been accessed 1,452 times. This page was last modified 02:09, 16 December 2007. Content is available under XOOPS Project.


Developers for Hire
Developers for Hire
Local Support Sites
Make a donation
Please select an amount to donate


Do you want your username revealed with your donation?
Yes - List me as a Generous Donor
No - List my donation as from an Anonymous Donor


Powered by
XOOPS Code hosted on SourceForge

Powered by PHP

PHP 5

Powered by MySQL

Powered by Smarty

OSI certified

GPL

All content on this site is subject to the Creative Commons License
Top Tags
Theme (5) news (2) security (2) sport (1) Arabic (1) wiki (1) Christmas (1) jQuery (1) tag (1) module (1) SEO (1) comments (1)
Advertisement