1
sarahmx
Help with custom module search.inc.php
  • 2013/4/6 12:49

  • sarahmx

  • Quite a regular

  • Posts: 381

  • Since: 2007/10/28


Hi XOOPSers im a php newbie

..i have converted my php simple directory application to xoops module but im stuck with xoops search

i have this search query in my page module... which is working fine

global $xoopsDB;
    
$result "SELECT * FROM ".$xoopsDB->prefix("directory_info")." where name like '%" .  $_POST['search']  .  "%' or department   like '%" .  $_POST['search']  .  "%' or unit like '%" .  $_POST['search']  .  "%'  or position like '%" .  $_POST['search']  .  "%' ORDER BY name";


but now i want to integrate the xoops search (mysite.com/search.php) in my module instead of using own search in my php page

what i did so far

in xoops_version.php

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



function directory_search()

{

    global 
$xoopsDB;
    
$result "SELECT * FROM ".$xoopsDB->prefix("directory_info")." where name like '%" .  $_POST['search']  .  "%' or department   like '%" .  $_POST['search']  .  "%' or unit like '%" .  $_POST['search']  .  "%'  or position like '%" .  $_POST['search']  .  "%' ORDER BY name";
    
    
    
    
$ret = array();
    
$i 0;
     while(
$myrow $xoopsDB->fetchArray($result)){
        
$ret[$i]['image'] = "images/size2.gif";
        
$ret[$i]['link'] = "staff.php?cid=".$myrow['log_id']."";
        
$ret[$i]['title'] = $myrow['name'];
        
$i++;
    }
    return 
$ret;

}


can someone help me to correct my code

i just want to be able to search by name, position, department and unit in the xoops search

and the search result to show matching name in the directory

without showing name of submitter (user id)...


sorry for my english

2
zyspec
Re: Help with custom module search.inc.php
  • 2013/4/6 13:17

  • zyspec

  • Module Developer

  • Posts: 1095

  • Since: 2004/9/21


You were close... Here's a routine that should work. The main thing that you didn't do in your code was to actually retrieve the data ($xoopsDB->query()).

function directory_search($queryarray$andor$limit$offset$userid


    global 
$xoopsDB;

    
$sql "SELECT * FROM " $GLOBALS['xoopsDB']->prefix("directory_info") ."";

    
// 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 .= " AND ((department LIKE '%{$queryarray[0]}%' OR unit LIKE '%{$queryarray[0]}%' OR position LIKE '%{$queryarray[0]}%')";
        for ( 
$i=1$i<$count$i++ ) {
            
$sql .= $andor ";
            
$sql .= "(department LIKE '%{$queryarray[$i]}%' OR unit LIKE '%{$queryarray[$i]}%' OR position LIKE '%{$queryarray[$i]}%')";
        }
        
$sql .= ") ";
    }
    
$sql .= " ORDER BY name ASC";

    
$result $xoopsDB->query($sqlintval($limit), intval($offset));
    
$ret = array();
    
$i 0;
     while(
$myrow $xoopsDB->fetchArray($result)){ 
        
$ret[$i]['image'] = "images/size2.gif"
        
$ret[$i]['link'] = "staff.php?cid=".$myrow['log_id'].""
        
$ret[$i]['title'] = $myrow['name']; 
        
$i++; 
    } 
    return 
$ret
}

3
sarahmx
Re: Help with custom module search.inc.php
  • 2013/4/6 14:05

  • sarahmx

  • Quite a regular

  • Posts: 381

  • Since: 2007/10/28


Hye Zyspec...

Thank you for your reply but its not working..no search result is found

4
sarahmx
Re: Help with custom module search.inc.php
  • 2013/4/6 14:29

  • sarahmx

  • Quite a regular

  • Posts: 381

  • Since: 2007/10/28



search by name, department, unit, position not working no search result found..currently the directory have more than 50+ records

function directory_search($queryarray$andor$limit$offset$userid


    global 
$xoopsDB;

    
$sql "SELECT * FROM " $GLOBALS['xoopsDB']->prefix("directory_info") ."";

    
// 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 .= " AND ((name LIKE '%{$queryarray[0]}%' OR department LIKE '%{$queryarray[0]}%' OR unit LIKE '%{$queryarray[0]}%' OR position LIKE '%{$queryarray[0]}%')";
        for ( 
$i=1$i<$count$i++ ) {
            
$sql .= $andor ";
            
$sql .= "(name LIKE '%{$queryarray[$i]}%' OR department LIKE '%{$queryarray[$i]}%' OR unit LIKE '%{$queryarray[$i]}%' OR position LIKE '%{$queryarray[$i]}%')";
        }
        
$sql .= ") ";
    }
    
$sql .= " ORDER BY name ASC";

    
$result $xoopsDB->query($sqlintval($limit), intval($offset));
    
$ret = array();
    
$i 0;
     while(
$myrow $xoopsDB->fetchArray($result)){ 
        
$ret[$i]['image'] = "images/size2.gif"
        
$ret[$i]['link'] = "staff.php?cid=".$myrow['log_id'].""
        
$ret[$i]['title'] = $myrow['name']; 
        
$i++; 
    } 
    return 
$ret
}

5
zyspec
Re: Help with custom module search.inc.php
  • 2013/4/6 14:54

  • zyspec

  • Module Developer

  • Posts: 1095

  • Since: 2004/9/21


Here's an updated routine. The previous one didn't have 'name' column included. Please turn on debug and look at both "Errors" and the "Queries" to make sure there's no errors showing up.

function directory_search($queryarray$andor$limit$offset$userid)  
{  

    global 
$xoopsDB

    
$sql "SELECT * FROM " $GLOBALS['xoopsDB']->prefix("directory_info") .""

    
// 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 .= " AND ((name LIKE '%{$queryarray[0]}%' OR department LIKE '%{$queryarray[0]}%' OR unit LIKE '%{$queryarray[0]}%' OR position LIKE '%{$queryarray[0]}%')";
        for ( 
$i=1$i<$count$i++ ) { 
            
$sql .= $andor "
            
$sql .= "(name LIKE '%{$queryarray[$i]}%' OR department LIKE '%{$queryarray[$i]}%' OR unit LIKE '%{$queryarray[$i]}%' OR position LIKE '%{$queryarray[$i]}%')";
        } 
        
$sql .= ") "
    } 
    
$sql .= " ORDER BY name ASC"

    
$result $xoopsDB->query($sqlintval($limit), intval($offset)); 
    
$ret = array(); 
    
$i 0
     while(
$myrow $xoopsDB->fetchArray($result)){  
        
$ret[$i]['image'] = "images/size2.gif";  
        
$ret[$i]['link'] = "staff.php?cid=".$myrow['log_id']."";  
        
$ret[$i]['title'] = $myrow['name'];  
        
$i++;  
    }  
    return 
$ret;  
}

6
sarahmx
Re: Help with custom module search.inc.php
  • 2013/4/6 15:26

  • sarahmx

  • Quite a regular

  • Posts: 381

  • Since: 2007/10/28


i got this error

0.000281 SELECT FROM directory_info AND ((name LIKE '%David%' OR department LIKE '%David%' OR unit LIKE '%David%' OR position LIKE '%David%')) ORDER BY name ASC LIMIT 05
Error number
1064
Error message
You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near 'AND ((name LIKE '%David%' OR department LIKE '%David%' OR unit LIKE '%David%' OR p' at line 1

7
sarahmx
Re: Help with custom module search.inc.php
  • 2013/4/6 15:38

  • sarahmx

  • Quite a regular

  • Posts: 381

  • Since: 2007/10/28


i tried changing
%{$queryarray[0]}%


to
%$queryarray[0]%


still syntax error..

8
zyspec
Re: Help with custom module search.inc.php
  • 2013/4/6 16:00

  • zyspec

  • Module Developer

  • Posts: 1095

  • Since: 2004/9/21


sarahmx,

Thanks for showing the errors... dumb mistake sorry!

This should work better:

function directory_search($queryarray$andor$limit$offset$userid)   
{   

    global 
$xoopsDB;  

    
$sql "SELECT * FROM " $GLOBALS['xoopsDB']->prefix("directory_info") ."";  

    
// 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 department LIKE '%{$queryarray[0]}%' OR unit LIKE '%{$queryarray[0]}%' OR position LIKE '%{$queryarray[0]}%')";
        for ( 
$i=1$i<$count$i++ ) {  
            
$sql .= $andor ";  
            
$sql .= "(name LIKE '%{$queryarray[$i]}%' OR department LIKE '%{$queryarray[$i]}%' OR unit LIKE '%{$queryarray[$i]}%' OR position LIKE '%{$queryarray[$i]}%')";
        }  
        
$sql .= ") ";  
    }
    
$sql .= " ORDER BY name ASC";  

    
$result $xoopsDB->query($sqlintval($limit), intval($offset));  
    
$ret = array();  
    
$i 0;  
     while(
$myrow $xoopsDB->fetchArray($result)){   
        
$ret[$i]['image'] = "images/size2.gif";   
        
$ret[$i]['link'] = "staff.php?cid=".$myrow['log_id']."";   
        
$ret[$i]['title'] = $myrow['name'];   
        
$i++;   
    }   
    return 
$ret;   
}

9
sarahmx
Re: Help with custom module search.inc.php
  • 2013/4/6 16:12

  • sarahmx

  • Quite a regular

  • Posts: 381

  • Since: 2007/10/28


i didnt see it either..

now finally its working... thank you zyspec




10
zyspec
Re: Help with custom module search.inc.php
  • 2013/4/6 17:32

  • zyspec

  • Module Developer

  • Posts: 1095

  • Since: 2004/9/21


Glad it worked for you... Just feel bad that it took me so many attempts at something I should have been able to do in one pass.

Login

Who's Online

303 user(s) are online (177 user(s) are browsing Support Forums)


Members: 0


Guests: 303


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits