rcouser Posted May 6, 2008 Share Posted May 6, 2008 Hello I have been working on a search engine for my site over the last few days, but I just can't figure it out. When I search with empty field I get a page of //1< //1b //1> When I search for something I know is in the database it gets replaced by //1 Need some help please my brain is fried. Thanks in advance <?php // remove backslashes from the $_GET array include('corefuncs.php'); nukeMagicQuotes(); //specify how many results to display per page $limit = 10; // Get the search variable from URL $var = @$_GET['keywords'] ; //trim whitespace from the stored variable $trimmed = trim($var); //separate key-phrases into keywords $trimmed_array = explode(" ",$trimmed); // check for an empty string and display a message. if ($trimmed == "") { $resultmsg = '<p class="warning">Please enter a search...</p>' ; } // check for a search parameter if (!isset($var)){ $resultmsg = '<p class="warning">We do not seem to have a search parameter</p>' ; } // Build SQL Query for each keyword entered foreach ($trimmed_array as $trimm){ // create a connection to MySQL $conn = dbConnect('query'); // prepare SQL to retrieve $searchquery = "SELECT * FROM sr_listing WHERE listing_title LIKE '%$trimm%' OR listing_description like '%$trimm%' ORDER by listing_starttime ASC"; // Execute the query to get number of rows that contain search keywords $numresults = $conn->query($searchquery); $searchrow_num_links_main = $numresults->num_rows; // next determine if 's' has been passed to script, if not use 0. // 's' is a variable that gets set as we navigate the search result pages. if (empty($s)) { $s = 0; } // now let's get results. $searchquery .= " LIMIT $s,$limit" ; $numresults = $conn->query($searchquery) or die ( "Couldn't execute query" ); $searchrow = $numresults->fetch_assoc(); //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. do{ $adid_array[] = $searchrow[ 'listing_id' ]; }while( $searchrow = $numresults->fetch_assoc()); } //end foreach if($searchrow_num_links_main == 0 && $searchrow == 0){ $resultmsg = '<p class="warning">Search results for: <u>'. $trimmed.'</u></p><p class="warning">Sorry, your search returned zero results</p>' ; } //delete duplicate record id's from the array. To do this we will use array_unique function $tmparr = array_unique($adid_array); $i=0; foreach ($tmparr as $v) { $newarr[$i] = $v; $i++; } ?> <?php // display what the person searched for. if( isset ($resultmsg)){ echo $resultmsg; }else{ echo "Search results for: " . $trimmed; } foreach($newarr as $value){ // create a connection to MySQL $conn = dbConnect('query'); // prepare SQL to retrieve $query_value = "SELECT * FROM sr_listing WHERE listing_id = '$value'"; $num_value = $conn->query($query_value); $searchrow_linkcat = $num_value->fetch_assoc(); $searchrow_num_links = $num_value->num_rows; //now let's make the keywords bold. To do that we will use preg_replace function. $titlehigh = preg_replace ( "'($var)'si" , "<b> //1</b>" , $searchrow_linkcat[ 'listing_title' ] ); $linkhigh = preg_replace ( "'($var)'si" , "<b> //1</b>" , $searchrow_linkcat[ 'listing_description' ] ); foreach($trimmed_array as $trimm){ if($trimm != 'b' ){ $titlehigh = preg_replace( "'($trimm)'si" , "<b> //1</b>" , $titlehigh); $linkhigh = preg_replace( "'($trimm)'si" , "<b> //1</b>" , $linkhigh); } //end highlight ?> <p> <?php echo $titlehigh; ?><br /> <?php echo $linkhigh; ?><br /> </p> <?php } //end foreach $trimmed_array if($searchrow_num_links_main > $limit){ // next we need to do the links to other search result pages if ($s>=1) { // do not display previous link if 's' is '0' $prevs=($s-$limit); echo "<div align='left'><a href='$PHP_SELF?s=$prevs&q=$var&catid=$catid'>Previous " .$limit. "</a></div>"; } // check to see if last page $slimit =$s+$limit; if (!($slimit >= $searchrow_num_links_main) && $searchrow_num_links_main!=1) { // not last page so display next link $n=$s+$limit; echo "<div align='right'><a href='$PHP_SELF?s=$n&q=$var&catid=$catid'>Next " .$limit. "</a></div>"; } } } //end foreach $newarr ?> Link to comment https://forums.phpfreaks.com/topic/104456-php-mysql-search-engine/ Share on other sites More sharing options...
rcouser Posted May 7, 2008 Author Share Posted May 7, 2008 Can no one help me,please Link to comment https://forums.phpfreaks.com/topic/104456-php-mysql-search-engine/#findComment-535427 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.