Jump to content

Creating PHP Search Script...


jj20051

Recommended Posts

One of my clients asked me to build a simple search engine script for their site. The problem is now he wants to make it get better results. At the moment it searches just for those keywords entered. ( Example: If you type in free hosting, "Free web hosting" won't come up, only sites with "free hosting" joined together like that... Please forgive me for my somewhat messy code. Config.php connects to the database.

 

Example1: http://apenex.net/search.php?search=free%20hosting

Example2: http://apenex.net/search.php?search=free%20web%20hosting

 

As you see bravenet should come up on both queries, but it doesn't. It only comes up on an exact match. So any suggestions you may have, i'm willing to try.

 

MY CODE:

<?php
include ('config.php');

$search = $_GET['search'];

if ($search == NULL){

echo '<center><strong>You Didn\'t Type Anything In!</center></strong>';

} else { 

$query = "SELECT * FROM sites WHERE keywords LIKE '%".$search."%' ORDER BY totalrating DESC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{

$yourtext = $row['description'];
$title = $row['title'];
$url = $row['url'];
if ( strlen( $yourtext ) > 45 ) {
   $yourtext = explode( ' ',$yourtext );
   $yourtext = array_slice( $yourtext, 0, 45 );
   $yourtext = implode( ' ', $yourtext) . ' ...';
} 
else {
}

if ( strlen( $title ) > 11 ) {
   $title = explode( ' ',$title );
   $title = array_slice( $title, 0, 11 );
   $title = implode( ' ', $title) . ' ...';
}
else {
}
echo '<table border="0" class="sample">';
echo '<tr><td><strong><a href='.$url.'><font size="2">';
echo $title;
echo '</font></a></strong></td></tr>';
echo '</table>';
echo '<table width="580" border="0" class="sample">';
echo '<tr><td><font size="2">';
echo $yourtext;
echo '</font></td></tr><tr><td><font color="green" size="2">';
echo $url;
echo '</font></td></tr></table></font><br>';
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/146730-creating-php-search-script/
Share on other sites

The way you had it originally is correct.

 

What you want to do is explode the search criteria at the space, then create a query based off of that.

 

$search = $search . " "; // add a space so in case of 1 word this should work.
$searchArr = explode(" ", $search); // split variables into separate words.
$search = array();
foreach ($searchArr as $keyword) {
    if (!empty($keyword)) {
         $search[] = "`keywords` LIKE '%{$keyword}%'";
   }
}
if (count($search) > 1)
    $search = "(" . implode(" AND ", $search) . ")";
else
    $search = $search[0]; // no need for the implode.
$query = "SELECT * FROM sites WHERE {$search} ORDER BY totalrating DESC";

 

Give that a try and see what comes of it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.