Forum Moderators: coopster
<?php
$searchterm=$_POST['searchterm'];
$searchterm= trim($searchterm);
if (!$searchterm) {
echo 'You have not entered a search term. Please try again.';
exit;
}
if (get_magic_quotes_gpc()) {
$searchterm = stripslashes($searchterm);
}
$searchterm = mysql_real_escape_string($searchterm);
$query = "SELECT * FROM site_data WHERE description LIKE '%$searchterm%' OR page_title LIKE '%$searchterm%' OR keywords LIKE '%$searchterm%'";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
echo "<p>Number of Results found for <i><strong>$searchterm</strong></i>: $num_results</p>";
for ($i=0; $i <$num_results; $i++) {
$row = mysql_fetch_assoc($result);
echo '<p>'.($i+1).': ';
echo '<strong>' . stripslashes($row['page_title']) . '</strong>';
echo '<br />';
echo substr($row['description'], 0, 255) . '...';
echo '<br />';
echo stripslashes($row['url']);
echo '</p>';
}
?>
I tried to enter this line in:
keywords = explode(" ", $searchterm);
but it doesn't seem to work.
and do a search with a phrase where I know that if the words were searched independently I would get results here is my echoed query:
SELECT * FROM site_data WHERE description LIKE '%Array%' OR
page_title LIKE '%Array%' OR keywords LIKE '%Array%'
not sure where Array is coming from?
you would have to add each word to your query, maybe something like this
$query = "SELECT * FROM site_data WHERE description LIKE '%" . $keywords[0] . "%' OR page_title LIKE '%" . $keywords[0] . "%' OR keywords LIKE '%" . $keywords[0] . "%' OR description LIKE '%" . $keywords[1] . "%' OR page_title LIKE '%" . $keywords[1] . "%' OR keywords LIKE '%" . $keywords[1] . "%'";
you would need to construct this in a loop so it added the right amount for the number of words entered
multiple queries would be something like
$query = "SELECT * FROM site_data WHERE description LIKE '%" . $keywords[0] . "%' OR page_title LIKE '%" . $keywords[0] . "%' OR keywords LIKE '%" . $keywords[0] . "%'";
then pull those results into an array then do
$query = "SELECT * FROM site_data WHERE description LIKE '%" . $keywords[1] . "%' OR page_title LIKE '%" . $keywords[1] . "%' OR keywords LIKE '%" . $keywords[1] . "%'";
and pull those results
you would again need to loop it to do a query for each word
you might be best doing a query with both to start
if that doesn't return enough results then split the phrase (if needed) and query for each word individually