xXandarXx
12-24-2010, 12:49 AM
I have a working search script, but I need to modify it to search better.
Example: "Faith Hill" returns only "Faith Hill". I need both "Faith Hill" and Hill, Faith" to be returned no matter which way the user inputs the string.
Here's my code, any ideas?
<html>
<head>
<title>karaoke search script</title>
</head>
<body onLoad="form.q.focus()">
<form name="form" action="searcho.php" method="get">
<input type="text" name="q" />
<input type="submit" name="Submit" value="Search" />
</form>
<?php
// Get the search variable from URL
$var = @$_GET['q'] ;
$trimmed = trim($var); //trim whitespace from the stored variable
$trimmed = trim($trimmed, ",");
// rows to return
$limit=100;
// check for an empty string and display a message.
if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","username","password"); //(host, username, password)
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("songsdb") or die("Unable to select database"); //select which database we're using
// Build SQL Query
$query = "select * from songs where Title like \"%$trimmed%\" or Artist like \"%$trimmed%\"
order by Title"; // EDIT HERE and specify your table and field names for the SQL query
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// If we have no results, offer a google search as an alternative
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "<br>You searched for: "" . $var . """;
// begin to show results set
// echo "Results";
$count = 1 + $s ;
// now you can display the results returned
echo "<table border=1><TR><td> </TD><td><b>Title</b></TD><td><b>Artist</b></TD><td><b>Number</b></TD></TR>";
while ($row= mysql_fetch_array($result)) {
$title = $row["Title"];
$artist = $row["Artist"];
$number = $row["Number"];
echo "<tr><td>$count.)</td><td>$title</td><td>$artist</td><td>$number</td></tr>" ;
$count++ ;
}
echo "</table>";
$currPage = (($s/$limit) + 1);
//break before paging
echo "";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print "";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
echo "";
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<br>Showing results $b to $a of $numrows";
?>
</body>
</html>
Example: "Faith Hill" returns only "Faith Hill". I need both "Faith Hill" and Hill, Faith" to be returned no matter which way the user inputs the string.
Here's my code, any ideas?
<html>
<head>
<title>karaoke search script</title>
</head>
<body onLoad="form.q.focus()">
<form name="form" action="searcho.php" method="get">
<input type="text" name="q" />
<input type="submit" name="Submit" value="Search" />
</form>
<?php
// Get the search variable from URL
$var = @$_GET['q'] ;
$trimmed = trim($var); //trim whitespace from the stored variable
$trimmed = trim($trimmed, ",");
// rows to return
$limit=100;
// check for an empty string and display a message.
if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","username","password"); //(host, username, password)
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("songsdb") or die("Unable to select database"); //select which database we're using
// Build SQL Query
$query = "select * from songs where Title like \"%$trimmed%\" or Artist like \"%$trimmed%\"
order by Title"; // EDIT HERE and specify your table and field names for the SQL query
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// If we have no results, offer a google search as an alternative
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "<br>You searched for: "" . $var . """;
// begin to show results set
// echo "Results";
$count = 1 + $s ;
// now you can display the results returned
echo "<table border=1><TR><td> </TD><td><b>Title</b></TD><td><b>Artist</b></TD><td><b>Number</b></TD></TR>";
while ($row= mysql_fetch_array($result)) {
$title = $row["Title"];
$artist = $row["Artist"];
$number = $row["Number"];
echo "<tr><td>$count.)</td><td>$title</td><td>$artist</td><td>$number</td></tr>" ;
$count++ ;
}
echo "</table>";
$currPage = (($s/$limit) + 1);
//break before paging
echo "";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print "";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
echo "";
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<br>Showing results $b to $a of $numrows";
?>
</body>
</html>