I have a query results code that works just fine. The query is done twice in order to support the prev/next page results. I haven't figured out a way to change resolve that yet. Anyhow, I would like the search to be at a default "and" between search variables as opposed to "or" that it seems to be doing now.
If a there is a search for more than one word, everything with either word posts from query. I just want it to post results for records that contain all the search variables. Partial word matches are also important. I was able to refine results a bit by setting - %$keywords[$i]% to $keywords[$i]%
Would be greatful for any help on this and for any refinements,
James
/// Search Page ///
<form action=results.php method=get>
<input type=text name="Srch" size=20 maxlength=255>
<input type=submit value=Go>
</form>
/// Results Page ///
<?php
session_start();
session_id();
session_register("Class_1");
session_register("Class_2");
session_register("Class_3");
session_register("Class_4");
session_register("Class_5");
session_register("Class_6");
session_register("Cat_No");
session_register("Name");
session_register("Description_Page");
session_register("List_Cart");
session_register("Specials");
session_register("Srch");
?>
<?php
$server = "localhost";
$user = "";
$pass = "";
$database = "";
$conn = mysql_pconnect ($server,$user,$pass);
$select = mysql_select_db("$database")or die ("unable to select db");
$limit=10;
$keywords=split("[ ]{1,}", $Srch);
for($i=0; $i<count($keywords); $i++)
$numresults=mysql_query("select * from TABLE
WHERE Class_1 like '$keywords[$i]%' OR Class_2 like '$keywords[$i]%' OR Class_3 like '$keywords[$i]%' OR Class_4 like '$keywords[$i]%' OR Class_5 like '$keywords[$i]%' OR Class_6 like '$keywords[$i]%' OR Cat_No like '$keywords[$i]%' OR Name like '$keywords[$i]%' OR Description_Page like '$keywords[$i]%'
ORDER BY Sort_Order ASC, ID DESC");
$numrows=mysql_num_rows($numresults);
if (empty($offset))
{
$offset=0;
}
$keywords=split("[ ]{1,}", $Srch);
$query="SELECT * from TABLE WHERE ";
for($i=0; $i<count($keywords); $i++)
{
$query.="Class_1 like '$keywords[$i]%' OR Class_2 like '$keywords[$i]%' OR Class_3 like '$keywords[$i]%' OR Class_4 like '$keywords[$i]%' OR Class_5 like '$keywords[$i]%' OR Class_6 like '$keywords[$i]%' OR Name like '$keywords[$i]' OR Cat_No like '$keywords[$i]%' OR Description_Page like '$keywords[$i]%' OR ";
}
$query=substr($query, 0, -4);
$query.=" order by Sort_Order ASC, ID DESC limit $offset,$limit";
$result=mysql_query($query);
if ($row = mysql_fetch_array($result)) {
do {
print (" Results ... ");
} while($row = mysql_fetch_array($result));
} else {print "<br><br><b>Sorry, no product records were found!</b><br><br><br>";}
print "Search Result Pages: ";
$pages=intval($numrows/$limit);
if (($pages!=0 and $pages!=1) or ($pages==1 and ($numrows%$limit)))// If data <= 1 page skip all
{
if ($offset!=0)// bypass PREV link if offset is 0
{
$prevoffset=$offset-$limit;
echo "<a href=\"$PHP_SELF?&offset=$prevoffset\" onmouseover=\"window.status='Previous $limit Results'; return true\">« PREVIOUS</a> \n";
}
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit)// has remainder so add one page
{
$pages++;
}
for ($i=1;$i<=$pages;$i++) // loop thru and make links
{
$newoffset=$limit*($i-1);
If ($offset!=$newoffset) //Make a link only for other pages
{
echo "<a href=\"$PHP_SELF?&offset=$newoffset\" onmouseover=\"window.status='Page $i Results'; return true\">$i</a> \n";
}
else
{
print "$i \n";
}
}
// check to see if NEXT link is needed
if ($pages!=1 or (($pages==1 and ($numrows%$limit))))
{
if (($offset+$limit)<$numrows) // if last page skip NEXT link
{$newoffset=$offset+$limit;
echo "<a href=\"$PHP_SELF?&offset=$newoffset\" onmouseover=\"window.status='Next $limit Results'; return true\">NEXT »</a>\n";
}
}
}
?>