Forum Moderators: coopster
This was my little proto-search engine (where you search for a single word or a phrase, works great):
<?php(I've got four separate tables of articles, named categoryone, etc., for categories -- the user chooses one category from a dropdown menu before searching for keywords, hence those elseif statements.)
$searchterm= trim($searchterm);
if (!$category ¦¦!$searchterm)
{
echo 'You have not entered search details. Please go back and try again.';
exit;
}
$category = addslashes($category);
$searchterm = addslashes($searchterm);
$dbh=mysql_connect ("localhost", "username", "password") or die ("Cannot connect to the database");
mysql_select_db ("db_name", $dbh);
$query = "select * from $category where article like '%".$searchterm."%'";
$result = mysql_query($query) or die('Error on page');
$num_results = mysql_num_rows($result);
echo '<P>Number of articles found: '.$num_results.'<BR>';if ($category == 'categoryone') {
$cat='a';
}
elseif ($category == 'categorytwo') {
$cat='b';
}
elseif ($category == 'categorythree') {
$cat='c';
}
elseif ($category == 'categoryfour') {
$cat='d';
}for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo '<P><STRONG>'.($i+1).'. Date:</STRONG> ';
echo stripslashes($row['date']);echo '<BR><STRONG>Article:</STRONG> ';
echo stripslashes('<A HREF=http://mysite.com/index.php?'.$cat.'='.$row['id'].'>');
echo stripslashes($row['title'].'</A><BR>');
echo '<P>';
}
?>
I've tried using some examples of explode I've found, like so:
$search_array=explode(" ", $searchterm);I've also tried this:
$search_num=(integer)count($search_array);
... ... ... ... ...
$query = "select * from $category ";
if (isset($searchterm))
$query .= " WHERE article LIKE";
for($i=0; $i < count($searchterm);$i++)
{
$query .= " '%".$searchterm[$i]."%'";
if ($i < count($searchterm)-1)
{
$query .= " AND article LIKE ";
}
}
if ($searchterm) {But for some reason, whenever I search for even a single word with this, the results page comes up with a list of every article in the category, rather than just the articles with the keyword.
$searchterm = trim($searchterm);
$searchterm = explode(" ", $searchterm);
}
... ... ... ... ...
$query = "SELECT * FROM $category ";
if (isset($searchterm)) {
$query .= " WHERE "; for($i = 0; $i < count($searchterm); $i++) {
$query .= "article LIKE '%" . $searchterm[$i] . "%'"; if ($i < count($searchterm)-1) {
$query .= " AND "; }
}
}
I'm still a newbie at this and I'm trying to keep it as simple and uncomplicated as possible; I don't really care about search "relevance," I've looked around at the MATCH AGAINST and FULLTEXT stuff on mysql.com and it didn't seem to fit with what I'm trying to do. Much of the Googling I've done seems to show people having a whole separate table for keywords in their articles, and I really don't wanna do that either. Any suggestions?
foreach loop as opposed to the for loop to simply your code:
foreach ($searchterm as $term) {
$query .= " AND article LIKE '%$term%'";
}