02-25-2010, 06:58 PM
|
#4 (permalink)
|
|
The Wanderer
Join Date: Feb 2010
Posts: 5
Thanks: 0
|
Oh. You use such a simple script. I had to look for hundreds php tutorials to create PHP/MySQL search engine with search keywords displayed in bold
PHP Code:
< ?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 = "<P>Search Error</P><P>Please enter a search...</P>" ;
025 }
026
027 // check for a search parameter
028 if (!isset($var)){
029 $resultmsg = "<P>Search Error</P><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 = "<P>Search results for: ". $trimmed."</P><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 <P>
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 ? >
|
|
|
|