...

View Full Version : Search Script Bug.



Loic
01-09-2008, 11:02 PM
Hi all, I need some help/advice on a search script that I downloaded. It works perfectly when I search for a single word, but when I search for 2 words I get multiple responses. As I am new to php, and I did not write this script I am having trouble finding out why it is doing that and how to get around it. Any help would be greatly appreciated.

To view the bug I have script here http://dev.loic.net.au/search.php and try searching for “item water” and then for just “item” or “water”



<?php
include ("includes/global.php");

$template = new Template;
$template->load("includes/default.htm");

$connections = mysql_connect($host, $username, $password) or die ( "Unabale to connect to the database" );

mysql_select_db($db_name) or die ( "Unable to select database!" );

$limit = 10;

$var = @$_GET['q'] ;
$trimmed = trim($var);
$trimmed_array = explode(" ",$trimmed);

if ($trimmed == "") {
$resultmsg = "<p>Search Error</p><p>Please enter a search...</p>" ;
}

if (!isset($var)){
$resultmsg = "<p>Search Error</p><p>We don't seem to have a search parameter! </p>" ;
}

foreach ($trimmed_array as $trimm) {

$query = "SELECT * FROM items WHERE title LIKE \"%$trimm%\" OR mini_dec LIKE \"%$trimm%\" OR main_dec LIKE \"%$trimm%\" ORDER BY id" ;

$numresults=mysql_query ($query);
$row_num_links_main =mysql_num_rows ($numresults);

if (empty($s)) {
$s=0;
}

$query .= " LIMIT $s,$limit" ;
$numresults = mysql_query ($query) or die ( "Couldn't execute query" );
$row= mysql_fetch_array ($numresults);

do{
$adid_array[] = $row[ 'id' ];
}while( $row= mysql_fetch_array($numresults));
}

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>" ;
}

$tmparr = array_unique($adid_array);
$i=0;

foreach ($tmparr as $v) {
$newarr[$i] = $v;
$i++;
}

$body .= <<<EOD
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="get" action="search.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr><td colspan="3"><strong>Search </strong></td></tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="text" name="q" value="$var" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" value="Search"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<hr />
EOD;

if( isset ($resultmsg)) {
$body .= $resultmsg;
$template->replace("title", "Search");
$template->replace("meta", "");
$template->replace("body", $body);
$template->publish();
exit();
} else {
$body .= "Search results for: " . $var;
}

foreach($newarr as $value) {

$query_value = "SELECT * FROM items WHERE id = '$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[ 'title' ] );
$linkhigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'mini_dec' ] );
$linkdesc = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat[ 'main_dec' ] );

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);
$linkdesc = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $linkdesc);
}
$itemID = $row_linkcat['id'];
$body .= <<<EOD
<p><a href="item.php?id=$itemID">$titlehigh</a><br>
$linkhigh<br>
$linkdesc</p>
EOD;
}
if($row_num_links_main > $limit) {
if ($s>=1) {
$prevs=($s-$limit);
$body .= "<div align='left'><a href='$PHP_SELF?s=$prevs&q=$var&catid=$catid'>Previous " .$limit. "</a></div>";
}

$slimit =$s+$limit;
if (!($slimit >= $row_num_links_main) && $row_num_links_main!=1) {
$n=$s+$limit;
$body .= "<div align='right'><a href='$PHP_SELF?s=$n&q=$var&catid=$catid'>Next " .$limit. "</a></div>";
}
}
}
$template->replace("title", "Search");
$template->replace("meta", "");
$template->replace("body", $body);
$template->publish();
?>

CFMaBiSmAd
01-09-2008, 11:21 PM
The code specifically searches for all rows with any of the entered words. It is doing an "item OR water" because of the multiple queries and the array_unique() function call.

What do you want it to do?

Edit: Before you just edited something on your site, the code was displaying both the title/short description and the long description, I would guess with different id's for each.

I would recommend displaying what $adid_array and $newarr contain to make sure they are what you expect.

Loic
01-09-2008, 11:31 PM
Well I want it to not return multiple results. So if I do a search for “item water” it to return

1) water
2) item 1
3) item 2
4) item 3
not
1) water
2) water
3) item 1
4) item 1
5) item 2
6) item 2
7) item 3
8) item 3

CFMaBiSmAd
01-10-2008, 12:54 AM
I found your logic error that is causing this (you need to use an editor that lets you match up { } - but closer examination of the indenting also shows it. A comment on closing }, indicating what they go with will help too, especially after an amount of time goes by or someone else needs to read your code) -


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);
$linkdesc = preg_replace( "'($trimm)'si" , "<b>\\1</b>" , $linkdesc);
}
$itemID = $row_linkcat['id'];
$body .= <<<EOD
<p><a href="item.php?id=$itemID">$titlehigh</a><br>
$linkhigh<br>
$linkdesc</p>
EOD;
} In the code above, the last } shown is the end of the foreach() loop. This means that when $trimmed_array has two or more words, you get two or more identical lines concatenated onto $body.

The last } shown in the above code should be moved up so it is right under the previous }.

Loic
01-10-2008, 01:15 AM
Oh right, i see what you mean. Thanks heaps, thats all working now. that would of taken me ages to find :)

by the way, what PHP IDE would you recommend? at the moment i am using Dreamweaver. which seems to be ok???



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum