/    Sign up×
Community /Pin to ProfileBookmark

My Search Not Working How I Would Like

Here is a search that I am making to see my problem view this: [url]http://www.d-top.org/search/?q=a&Submit=Search[/url]

Now take one of the descriptions, and mess the words up a little, in the search box it should find what you searched for, but it doesn’t. Any idea how to make my code do that?

You also can’t take a keyword, and mix it with a something that isn’t a key word. For example Newgrounds is on the list for the title, and the one of the keywords for it is flash. You can’t search for “newgrounds flash”, because it will find nothing.

Here is the code:

[code=php]<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta name=”robots” content=”noindex,nofollow”>
<link rel=”stylesheet” type=”text/css” href=”style.css”>
<title>Finder</title>
</head>
<body OnLoad=”document.form.q.focus();”>
<p>&nbsp;</p>
<center>
<a href=”/search/”><img alt=”finder” src=”images/logo_finder.gif”></a><sup>Beta</sup><br>
<form name=”form” action=”<?$PHP_SELF?>” method=”get”>
<input type=”text” name=”q” />
<input type=”submit” name=”Submit” value=”Search” /><br>
<sup><a href=”addself.php”>Submit A Site</a></sup>
</form>
</center>

<?php

// Get the search variable from URL

$var = @$_GET[‘q’] ;
$trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10;

// check for an empty string and display a message.
if ($trimmed == “”)
{
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”,”dtoporg_dtop”,”abc123″); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db(“dtoporg_search”) or die(“Unable to select database”); //select which database we’re using

// Build SQL Query
$query = “SELECT * FROM search WHERE CONCAT(urltitle,description,keywords) LIKE ‘%$trimmed%’
ORDER BY keywords, description, urltitle”;

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

// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}

echo ‘<div class=”content”>’;
// get results
$query .= ” limit $s,$limit”;
$result = mysql_query($query) or die(“Couldn’t execute query”);

$count = 1 + $s;
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;

// display what the person searched for
echo “<p>Your search for <b>” . $var . “</b> found ” . $numrows . ” results</p>”;

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$url = $row[“url”];
$urltitle = $row[“urltitle”];
$menu1 = $row[“menu1”];
$description = $row[“description”];
$keywords = $row[“keywords”];
$count = 1 + $s ;
echo ‘<p><b><a href=”‘.$url.'”>’. $urltitle .'</a></b> &nbsp;&nbsp;&nbsp;Category: ‘. $menu1 .'<br>’. $description.'<br><span class=”url”>’.$url.'</span>’;
$count++ ;
}

$currPage = (($s/$limit) + 1);

//break before paging
echo “<p>&nbsp;</p>”;

// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print ” &nbsp;<a href=”$PHP_SELF?s=$prevs&q=$var”>&lt;&lt;
Prev 10</a>&nbsp&nbsp;”;
}

// 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!=0) {

// not last page so give NEXT link
$news=$s+$limit;
echo “&nbsp;<a href=”$PHP_SELF?s=$news&q=$var”>Next 10 &gt;&gt;</a>”;
}

$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo “<p>Showing results $b to $a of $numrows</p>”;

?>
</div>
</body>
</html>[/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@chazzyMar 24.2006 — Have you looked into mysql's FULL TEXT SEARCH? http://dev.mysql.com/doc/refman/4.1/en/fulltext-query-expansion.html

You might need to parse your input to replace spaces with wildcards as well.
Copy linkTweet thisAlerts:
@The_Little_GuyauthorMar 24.2006 — So... How would I put that into my code?
Copy linkTweet thisAlerts:
@chazzyMar 24.2006 — which part?

implementing full text search requires you to add the fulltext key on a set of columns in your table. then formatting the query to use it - most people use binary mode.

adding the wildcards is just using str_replace
Copy linkTweet thisAlerts:
@The_Little_GuyauthorMar 24.2006 — Ok, thanks for the Idea, I got it to work! I love it.
Copy linkTweet thisAlerts:
@bokehMar 25.2006 — implementing full text search requires you to add the fulltext key on a set of columns in your table.[/QUOTE]Full text queries can be run on tables without a full text index but are less efficient (whatever that means).

[I]Little Guy[/I], I would be interested to see your query.
Copy linkTweet thisAlerts:
@chazzyMar 25.2006 — If you don't create the index, it creates it temporarily after the first query and remains in there while the query is cached.

At least, that's my understanding of it. It's better to just make the index anyways, your DBA will be much happier with you.
Copy linkTweet thisAlerts:
@bokehMar 26.2006 — [B]Off topic:[/B] So what happens if one of the columns in the query is not included in the fulltext index?
×

Success!

Help @The_Little_Guy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...