Click to See Complete Forum and Search --> : Why do my search results duplicate?


sac8513
12-11-2007, 11:01 PM
If a store name is searched that has more than one word, the amount of results that i get equals the amount of words in the name.

For instance if i search "Justins Hardware" this is the search result:

Justins Hardware

Justins Hardware


but if i just search "Justins" I appropriately get:

Justins Hardware

Here is the part of the code that I believe has the issues


$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{
//EDIT HERE and specify your field name that is primary key
$adid_array[] = $row[ 'store_name' ];
}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 we will use array_unique function
$tmparr = array_unique($adid_array);
$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
?>

<form action="test.php" method="get" name="search">
<div align="center">
<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;
}

foreach($newarr as $value){


$query_value = "SELECT * FROM store WHERE store_name = '$value'";
$num_value=mysql_query ($query_value);
$row_linkcat= mysql_fetch_array ($num_value);
$row_num_links= mysql_num_rows ($num_value);

$titlehigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'store_name' ] );

foreach($trimmed_array as $trimm){
if($trimm != 'b' ){

$titlehigh = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $titlehigh);
}
//end highlight


I appreciate anyones help

gomisute
12-12-2007, 02:43 AM
looks like you're looping through your finds...for every keyword that is used in the search...(not sure...just glimpsed over your code).

bokeh
12-12-2007, 04:31 AM
You need to work on your query. Why do you need more than one?

sac8513
12-12-2007, 07:36 AM
I think because on the main search page I run the function and then when the results show I need to provide the function again so people can continue searching...

What do you suggest?

sac8513
12-12-2007, 07:38 AM
But even so it doesn't necessarily explain why my function would run fine as long as it is a one word store name??

bokeh
12-12-2007, 02:27 PM
But even so it doesn't necessarily explain why my function would run fine as long as it is a one word store name??Don't you think it would be better to start with 5-10 lines of efficient code with good logic rather than be chasing an error in inefficient code with flawed logic?

Even if you want to paginate your results you still only need one query.

sac8513
12-12-2007, 02:46 PM
Of course, efficiency is always better. However, I am pretty new to all of this and would like to get the function onto my site. I think I have done a pretty good job so far with some other members help.

Are you able to help me with a solution?

bokeh
12-12-2007, 02:58 PM
Are you able to help me with a solution?Maybe, but you will need to provide more info (about the table etc). And the code before this section.

sac8513
12-12-2007, 03:08 PM
ok well the table has a primary key colum , a column "store_name" , and there will be a third column "link" so my results will be hyperlinks to other pages ( that will be resolved later) . At a maxium this table will probably only have 500 entries.

The full code can be found here

http://webdeveloper.com/forum/showthread.php?t=167558&highlight=searching+database ---> POST #5

Or i can post it here

It includes code notes as well. After this step I will be looking into being able to insert this into html so i can incorporate it with my site design.

Let me know if there is any other info that you need. Thanks a lot