jasonc Posted June 22, 2006 Share Posted June 22, 2006 below is the script i am using to retrive and display the results of a search, the third line... $var....gets the details of the search query, and stripstrings it of all none readable characters, only letters numbers spaces.this should then do a search and display them in alph-orderit does but i have double results returned, when searching for lets say'mary had lamb'it shows the two results one of which is highlighted as requested the other is not highlighted.this i think is because it is not deleting the copies it finds, where details appear in the same entry twice. the part i think is wrong is where it should delete the doubles.....$tmparr = array_unique($adid_array); //delete duplicate record id's from the array. To do this we will use array_unique function$i=0;foreach ($tmparr as $v) {$newarr[$i] = $v;$i++;}maybe something is up with this part?below is the full script...please can someone take a look and help fix this problem for me, i'd be very grateful for your help.cheers[code]<?$limit = 10; //specify how many results to display per page$var = @stripstring($_POST[searchquery]); // Get the search variable from URL$trimmed = trim($var); //trim whitespace from the stored variable$trimmed_array = explode(" ",$trimmed); //separate key-phrases into keywordsif ($trimmed == "") { // check for an empty string and display a message. $resultmsg = "<p>Search Error</p><p>Please enter a search...</p>"; }if (!isset($var)){ // check for a search parameter $resultmsg = "<p>Search Error</p><p>We don't seem to have a search parameter! </p>"; }foreach ($trimmed_array as $trimm){ // Build SQL Query for each keyword entered $query = "SELECT * FROM items WHERE itemname LIKE \"%$trimm%\" OR description LIKE \"%$trimm%\" ORDER BY itemname ASC"; $numresults=mysql_query ($query); $row_num_links_main =mysql_num_rows ($numresults); if (empty($s)) { $s=0; } $query .= " LIMIT $s,$limit"; // now let's get results. $numresults = mysql_query ($query) or die ( "Couldn't execute query" ); $row= mysql_fetch_array ($numresults); do{ //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. $adid_array[] = $row[ 'itemid' ]; //EDIT HERE and specify your field name that is primary key }while( $row= mysql_fetch_array($numresults)); } //end foreachif($row_num_links_main == 0 && $row_set_num == 0){ $resultmsg = "<p>Search results for:" . $trimmed ."</p><p>Sorry, your search returned zero results</p>";} $tmparr = array_unique($adid_array); //delete duplicate record id's from the array. To do this we will use array_unique function $i=0; foreach ($tmparr as $v) { $newarr[$i] = $v; $i++; }// now you can display the results returned. But first we will display the search form on the top of the page// display what the person searched for. if( isset ($resultmsg)){ echo $resultmsg; exit(); }else{ echo "Search results for: " . $var; }foreach($newarr as $value){$query_value = "SELECT * FROM items WHERE itemid = '$value'"; // EDIT HERE and specify your table and field names for the SQL query$num_value=mysql_query ($query_value);$row_linkcat= mysql_fetch_array ($num_value);$row_num_links= mysql_num_rows ($num_value);//now let's make the keywods bold. To do that we will use preg_replace function. EDIT parts of the lines below that have fields names like $row_linkcat[ 'field1' ] This script assumes you are searching only 3 fields. If you are searching more fileds make sure that add appropriate line. $itemname = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'itemname' ] ); $itemdes = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'description' ] ); //$linkdesc = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'field2' ] );foreach($trimmed_array as $trimm){ if($trimm != 'b' ){ $itemname = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $itemname); //IF you added more fields to search make sure to add them below as well. $itemdes = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $itemdes); //$linkdesc = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $linkdesc); } //end highlight?><p><?php echo $itemname; ?><br><?php echo $itemdes; ?><br><?php //echo $linkdesc; ?></p><?php} //end foreach $trimmed_array if($row_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>"; } $slimit =$s+$limit; // check to see if last page if (!($slimit >= $row_num_links_main) && $row_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?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12629-my-search-results-are-doubled-up/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.