General Community > Scripting Help
Search Algorithms
(1/1)
Big P:
I've never written a search algorithm before, but took a stab at one today:
--- Code: ---<style type="text/css">
.highlight {
background-color: yellow;
font-weight: bold;
color: black;
}
</style>
<form method="post" action="search.php">
<input type="text" name="query" /><br />
<input type="submit" name="Submit!" />
</form>
<br />
<br />
<?
$qs = mysql_query('SELECT subject, titletext, maintext FROM phpnews_news WHERE subject RLIKE "{<{:<:]]' . $_POST['query'] . '{<{:>:]]" OR titletext RLIKE "' . $_POST['query'] . '" OR maintext RLIKE "' . $_POST['query'] . '" LIMIT 5');
while($results = mysql_fetch_array($qs))
{
/* Highlight Search Words */
$results['subject'] = str_replace($_POST['query'], '<b class="highlight">' . $_POST['query'] . '</b>', $results['subject']);
$results['titletext'] = str_replace($_POST['query'], '<b class="highlight">' . $_POST['query'] . '</b>', $results['titletext']);
$results['maintext'] = str_replace($_POST['query'], '<b class="highlight">' . $_POST['query'] . '</b>', $results['maintext']);
echo '
<p>
<b>Subject:</b> ' , $results['subject'] , '<br />
<b>Titletext:</b> ' , $results['titletext'] , '<br />
<b>Maintext:</b> ' , $results['maintext'] , '<br />
</p>';
}
exit;
--- End code ---
Could anyone recommend any ways of improving this? Basically phpnews_news is where all the news is stored, and 'subject', 'titletext' & 'maintext' are the fields that are being searched. :)
[Unknown]:
Grr... someone messed up preparsecode... (probably me :$.)
Those {<{ are supposed to be two [ next to each other.
That looks fine... I see nothign wrong with it. Have you looked at the discussion on search engine methods in errm... *the place*?
-[Unknown]
Big P:
I will now! :D
Navigation
[0] Message Index
Go to full version