j8r8c8 Posted January 24, 2008 Share Posted January 24, 2008 Anybody have any idea why this script produces a duplicate result for each keyword entered into the search form? <html> <?php include ("../_borders/top.htm"); //This script searches 2 MySql fields $address = $_SERVER['PHP_SELF']; $hostname_logon = "****" ; $database_logon = "****" ; $username_logon = "****" ; $password_logon = "****" ; //open database connection $connections = mysql_connect($hostname_logon, $username_logon, $password_logon) or die ( "Unabale to connect to the database" ); //select database mysql_select_db($database_logon) or die ( "Unable to select database!" ); //specify how many results to display per page $limit = 20; // Get the search variable from URL $var = @$_GET['q'] ; //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>Search Error</p><p>Please enter a search...</p>" ; } // check for a search parameter if (!isset($var)){ $resultmsg = "<p>Search Error</p><p>We don't seem to have a search parameter! </p>" ; } // Build SQL Query for each keyword entered foreach ($trimmed_array as $trimm){ // SQL Query $query = "SELECT * FROM parts_search WHERE partno LIKE '%". $trimm ."%' OR description LIKE '%". $trimm ."%' ORDER BY partno" ; // Execute the query to get number of rows that contain search kewords $numresults=mysql_query ($query); $row_num_links_main = mysql_num_rows ($numresults); // 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. $query .= " LIMIT $s,$limit" ; $numresults = mysql_query ($query) or die ( "Couldn't execute query" ); $row= mysql_fetch_array ($numresults); //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[] = $row[ 'id' ]; }while( $row= mysql_fetch_array($numresults)); } //end foreach if($row_num_links_main == 0 && $row_set_num == 0){ $resultmsg = "<p>Search results for:" . $trimmed ."</p><p>Sorry, your search returned zero results</p>" ; } //delete duplicate record id's from the array. To do this I will use array_unique function $tmparr = array_unique($adid_array); $i=0; foreach ($tmparr as $v) { $newarr[$i] = $v; $i++; } // Display the results returned. But first we will display the search form on the top of the page ?> <p align="center"><b><span lang="en-us"><font size="5"><a href="../index.htm">Home</a> </font><font color="#0000FF" size="2">●</font><font size="5"> <a href="../search.htm">Search</a> </font><font color="#0000FF" size="2">●</font><font size="5"> <a href="../site_map.htm">Index</a> </font><font color="#0000FF" size="2">●</font><font size="5"> <a href="../New%20Parts.htm">New Parts</a> </font><font color="#0000FF" size="2">●</font><font size="5"> <a href="../Preowned-Parts.htm">Preowned-Parts</a> </font><font color="#0000FF" size="2">●</font><font size="5"> <a href="../christmas_ideasUnder30.htm">Christmas Ideas</a> </font><font color="#0000FF" size="2">●</font><font size="5"> <a href="../technical.htm">Technical</a> </font><font color="#0000FF" size="2">●</font><font size="5"> <a href="../Race_Team.htm"> Race Team</a> </font><font color="#0000FF" size="2">●</font><font size="5"> <a href="../winter_rebuild.htm">Winter Rebuild</a> </font><font color="#0000FF" size="2">●</font><font size="5"> <a href="../Contact_Us.htm">Contact Us</a></font></span></b></p> <form action="search_form.htm" method="get" name="search"> <div align="left"> <input name="q" type="text" value="<?php echo $q; ?>" size="15"> <input name="search" type="submit" value="Search"> </div> </form> <?php // display what the person searched for. if( isset ($resultmsg)){ echo $resultmsg; exit(); }else{ echo "Search results for: " . "$var"; echo "<br>"; echo "<br>"; } echo "<body background=\"../Logos/GoodVibeLogo1989rev.JPG\"> <table width=100%>"; foreach($newarr as $value){ $query_value = "SELECT * FROM parts_search WHERE id = '$value'"; $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. $url = $row_linkcat[ 'url' ]; $part_num = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'partno' ] ); $description = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'description' ] ); $reg_price = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'reg_price' ] ); $your_price = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'your_price' ] ); $weight = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'weight' ] ); $vendor = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'vendor' ] ); $url_f = sprintf ("%s",$url); $filename = sprintf("../Product-Scans-NonDesc/%s_small.JPG",$url_f); if (file_exists($filename)) { $pic = getimagesize($filename,$size); if ($pic[0] > $pic[1]) { $sizew = (80); $sizeh = round($pic[1] / $pic[0] * 80); } elseif ($pic[0] < $pic[1]) { $sizew = round($pic[0] / $pic[1] * 80); $sizeh = (80); } else if ($pic[0] == $pic[1]) { $sizew = (80); $sizeh = (80); } else { $sizew=$pic[0]; $sizeh=$pic[1]; } } foreach($trimmed_array as $trimm){ if($trimm != 'b' ){ $part_num = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $part_num); $description = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $description); } //end highlight ?> <?php echo "<tr><td rowspan=2 width=10%> <form action=../moreinfo.php method=post> <input type=image src=$filename height=$sizeh width=$sizew> <input type=\"hidden\" name=\"url\" value=\"$url_f\"> <input type=\"hidden\" name=\"partnumber\" value=\"$part_num\"> <input type=\"hidden\" name=\"desc\" value=\"$description\"> <input type=\"hidden\" name=\"yourprice\" value=\"$your_price\"> <input type=\"hidden\" name=\"weight\" value=\"$weight\"> <input type=\"hidden\" name=\"logo\" value=\"$vendor\"></form></td> <input type=\"hidden\" name=\"address\" value=\"$address\"></td> <td colspan=2> <font size=4 face=Verdana color=red><strong>$part_num</strong></font>   <font face=Arial>$description</font> <font color=blue size=5 face=Verdana><strong>\$$your_price</strong></font> </td></tr> <tr><td width=11% align=left> <form action=../moreinfo.php method=post> <input type=image src=\"../Scans/MOREINFO3.JPG\"> <input type=\"hidden\" name=\"url\" value=\"$url_f\"> <input type=\"hidden\" name=\"partnumber\" value=\"$part_num\"> <input type=\"hidden\" name=\"desc\" value=\"$description\"> <input type=\"hidden\" name=\"yourprice\" value=\"$your_price\"> <input type=\"hidden\" name=\"weight\" value=\"$weight\"> <input type=\"hidden\" name=\"logo\" value=\"$vendor\"></form></td> <input type=\"hidden\" name=\"address\" value=\"$address\"></td> <td align=left width=79%><form method=POST action=http://www.cartserver.com/sc/cart.cgi> <input type=hidden name=item value=\"a-7283^$part_num^$description^$your_price^1^^^^$weight^^\"> <input type=image name=add src=\"../Scans/BUYNOW3.JPG\"> <input type=hidden name=pictureurl value=http://www.goodvibesracing.com/Product-Scans/$url_f.JPG></form></td></tr> "; ?><?php } } //end foreach $newarr echo "</table>"; //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>"; } // check to see if last page $slimit =$s+$limit; 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>"; } } echo "There were $row_num_links_main returned results"; ?></html> Thanks in advance for any assistance. j8r8c8 Quote Link to comment https://forums.phpfreaks.com/topic/87590-php-search-page-problem/ 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.