SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict Normal75's Avatar
    Join Date
    Oct 2001
    Location
    Vancouver, Canada
    Posts
    245
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    search multiple fields

    i have a range of codes that only perform search on one field, my question is how would i alter the codes a bit so i can search 2 or maybe 3 fields? I played around with AND and OR and couldn't seem to get it to work so any help is appreciated. thanks.

    PHP Code:
      if ($HTTP_POST_VARS['keywordSearch']) {
        if (
    $HTTP_POST_VARS['KeyOpt'] == 3) {
               if (
    $HTTP_POST_VARS['keywordSearch']!="") {
                       
    $str=trim($HTTP_POST_VARS['keywordSearch']);
                       
    $sql_statement .= "and (LCASE($DBtableQuality.skills) regexp '".strtolower($str)."') ";
                   }
        }
        else {
                if (
    $HTTP_POST_VARS['KeyOpt'] == 1) {
                  
    $opKeyw 'and';
                } else {
                  
    $opKeyw 'or';
                }
                
    $first 1;
                if (
    $HTTP_POST_VARS['keywordSearch']!="") {
                 
    $str=trim($HTTP_POST_VARS['keywordSearch']);
                 
    $str eregi_replace("\\\\","\\\\",$str);
                 
    $str eregi_replace("\+","\\\\+",$str);
                 
    $str eregi_replace("\*","\\\\*",$str);
                 
    $str eregi_replace("\.","\\\\.",$str);
                 
    $str eregi_replace("\?","\\\\?",$str);
                 
    $str eregi_replace("\]","\\\\]",$str);
                 
    $str eregi_replace("\[","\\\\[",$str);
                 
    $str eregi_replace("\^","\\\\^",$str);
                 
    $str eregi_replace("\(","\\\\(",$str);
                 
    $str eregi_replace("\)","\\\\)",$str);
                 
    $str eregi_replace("\\\$","\\\\$",$str);
                 
    $str eregi_replace("'","\\'",$str);
                 
    $str eregi_replace("\|","%%or%%",$str);
                       while (
    eregi("(.*)[,| ](.*)",$str,$regs))
                          {
                           
    $regs[2] = trim($regs[2]);
                           if (!empty(
    $regs[2])) {
                               
    $regs[2] = eregi_replace("%%or%%","\\\\|",$regs[2]);
                               if (
    $first) {
                                      
    $first 0;
                                      
    $sql_statement .= "and (LCASE($DBtableQuality.skills) regexp '".strtolower($regs[2])."' ";
                               } else {
                                      
    $sql_statement .= "$opKeyw LCASE($DBtableQuality.skills) regexp '".strtolower($regs[2])."' ";
                                }
                           }
                           
    $str=$regs[1];
                           }
                           
    $str eregi_replace("%%or%%","\\\\|",trim($str));
                           if(!empty(
    $str)) {
                               if (
    $first) {
                                  
    $first 0;
                                   
    $sql_statement .= "and (LCASE($DBtableQuality.skills) regexp '".strtolower($str)."' ";
                               }
                               else {
                                   
    $sql_statement .= "$opKeyw LCASE($DBtableQuality.skills) regexp '".strtolower($str)."' ";
                               }
                          }
                          if (
    $first == 0) {
                             
    $sql_statement .= ") ";
                          }
                    }
               }
         } 
    ~It will come to me one day~

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Are you aware of Mysql's FULLTEXT index designed to help with searches?

    Might help you in this case.

    It creates and index of text in the 2 or 3 columns you choose, it will return results faster, and rank them far more intelligently than we could do.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2007
    Location
    Colorado Springs, CO
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure I understand exactly what your looking for but hopefully this is it:

    Code:
    mysql_query("SELECT * FROM a 
    WHERE Active = '1' 
    AND Tester LIKE 'b%' 
    AND Testy LIKE '%G%' 
    ORDER BY Test


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •