php search with tags

Gateway Lx6810-01 desktop
January 26, 2010 at 06:11:09
Specs: Windows XP, pentium 4
Hi guys, I am making a search for a site I am currently working on. The search will go through 10 categories and then tags. The categories are all drop downs so matching them is easy enough but the tags are a little tougher. I put them in two ways, one I have them is a comma list inside the same row as the item and I also created a seperate table that has the id of the item and each tag individually per row. I wasnt sure the best way to do it so i did both now i need to find the quickest method. The long way I figure it to take the users tags from the search form, explode them by comme then check each on individually and if there is a match create a second list of product ids based on that match then just do a second search for all the other categories and at the end do something like

&& id IN($ids)

I know there must be a quicker way. Any help would be appreciated.


See More: php search with tags

Report •

#1
February 9, 2010 at 04:26:02
Hi Friends!!!

Thanks for given PHP tags..infor...meet again.


Report •

#2
February 26, 2010 at 06:57:40
Using php tutorials I have found a script to create PHP/MySQL search engine with search keywords displayed in bold
< ?php   
002 $hostname_logon = "localhost" ;      
003 $database_logon = "databaseName" ;     
004 $username_logon = "databaseUser" ;     
005 $password_logon = "databasePass" ;      
006 //open database connection   
007  $connections = mysql_connect($hostname_logon, $username_logon, $password_logon) or die ( "Unabale to connect to the database" );   
008  //select database   
009  mysql_select_db($database_logon) or die ( "Unable to select database!" );   
010     
011 //specify how many results to display per page   
012 $limit = 10;   
013     
014 // Get the search variable from URL   
015   $var = @$_GET['q'] ;   
016   $s = $_GET['s'] ;   
017 //trim whitespace from the stored variable   
018   $trimmed = trim($var);    
019 //separate key-phrases into keywords   
020   $trimmed_array = explode(" ",$trimmed);   
021     
022 // check for an empty string and display a message.   
023 if ($trimmed == "") {   
024   $resultmsg =  "

Search Error</P>

Please enter a search...</P>" ;   
025   }   
026     
027 // check for a search parameter   
028 if (!isset($var)){   
029   $resultmsg =  "

Search Error</P>

We don't seem to have a search parameter! </P>" ;   
030   }   
031 // Build SQL Query for each keyword entered   
032 foreach ($trimmed_array as $trimm){   
033       // EDIT HERE and specify your table and field names for the SQL query   
034      $query = "SELECT * FROM tablename WHERE field1 LIKE '%$trimm%' OR field2 like '%$trimm%' OR field3 like '%$trimm%' ORDER BY field1  DESC" ;    
035      // Execute the query to  get number of rows that contain search kewords   
036      $numresults=mysql_query ($query);   
037      $row_num_links_main =mysql_num_rows ($numresults);   
038     
039      // next determine if 's' has been passed to script, if not use 0.   
040      // 's' is a variable that gets set as we navigate the search result pages.   
041      if (empty($s)) {   
042          $s=0;   
043      }   
044     
045       // now let's get results.   
046       $query .= " LIMIT $s,$limit" ;   
047       $numresults = mysql_query ($query) or die ( "Couldn't execute query" );   
048       $row= mysql_fetch_array ($numresults);   
049     
050       //store record id of every item that contains the keyword in the array we need to do this to avoid display of duplicate search result.   
051       do{   
052           $adid_array[] = $row[ 'fieldid' ];   
053       }while( $row= mysql_fetch_array($numresults));   
054  } //end foreach   
055     
056 if($row_num_links_main == 0 && $row_set_num == 0){   
057    $resultmsg = "

Search results for: ". $trimmed."</P>

Sorry, your search returned zero results</P>" ;   
058 }   
059    //delete duplicate record id's from the array. To do this we will use array_unique function   
060    $tmparr = array_unique($adid_array);   
061    $i=0;   
062    foreach ($tmparr as $v) {   
063        $newarr[$i] = $v;    
064        $i++;   
065    }   
066     
067 // now you can display the results returned. But first we will display the search form on the top of the page   
068 ? >   
069     
070 <FORM method=get name=search action=search.php>   
071   <DIV>   
072       <INPUT value=" < ?php echo $q; ? > " type=text name=q>   
073       <INPUT value=Search type=submit name=search>   
074   </DIV>   
075 </FORM>   
076     
077 < ?php   
078 // display what the person searched for.   
079  if( isset ($resultmsg)){   
080   echo $resultmsg;   
081   exit();   
082  }else{   
083   echo "Search results for: " . $var;   
084  }   
085      
086 foreach($newarr as $value){   
087  // EDIT HERE and specify your table and field names for the SQL query   
088 $query_value = "SELECT * FROM tablename WHERE fieldid = '$value'";   
089  $num_value=mysql_query ($query_value);   
090  $row_linkcat= mysql_fetch_array ($num_value);   
091  $row_num_links= mysql_num_rows ($num_value);   
092     
093 //now let's make the keywods bold. To do that we will use preg_replace function.   
094 //Replace field   
095   $titlehigh = preg_replace ( "'($var)'si" , "<STRONG> \1</STRONG>" , $row_linkcat[ 'field1' ] );   
096   $linkhigh = preg_replace ( "'($var)'si" , "<STRONG> \1</STRONG>" , $row_linkcat[ 'field2' ] );   
097   $linkdesc = preg_replace ( "'($var)'si" , "<STRONG> \1</STRONG>" , $row_linkcat[ 'field3' ] );   
098     
099 foreach($trimmed_array as $trimm){   
100     if($trimm != 'b' ){   
101         $titlehigh = preg_replace( "'($trimm)'si" ,  "<STRONG> \1</STRONG>" , $titlehigh);   
102         $linkhigh = preg_replace( "'($trimm)'si" , "<STRONG> \1</STRONG>" , $linkhigh);   
103         $linkdesc = preg_replace( "'($trimm)'si" ,  "<STRONG> \1</STRONG>" , $linkdesc);    
104      }   
105 //end highlight   
106     
107 ? >   
108  

   
109 < ?php echo $titlehigh; ? >   
110     
111 < ?php echo $linkhigh; ? >   
112     
113 < ?php echo $linkdesc; ? >   
114 </P>   
115      
116 < ?php   
117 }   //end foreach $trimmed_array    
118    if($row_num_links_main > $limit){   
119    // next we need to do the links to other search result pages   
120       if ($s>=1) { // do not display previous link if 's' is '0'   
121         $prevs=($s-$limit);   
122          echo "<DIV>< a href="$PHP_SELF?s=$prevs&q=$var&catid=$catid" >Previous " .$limit. "< /a >   
123 </DIV>";   
124       }   
125      // check to see if last page   
126      $slimit =$s+$limit;   
127        if (!($slimit >= $row_num_links_main) && $row_num_links_main!=1) {   
128      // not last page so display next link   
129           $n=$s+$limit;   
130            echo "<DIV>< a href="$PHP_SELF?s=$n&q=$var&catid=$catid">Next " .$limit. "< /a >   
131 </DIV>";   
132         }   
133     }   
134 }  //end foreach $newarr   
135 ? >  


Report •
Related Solutions


Ask Question