Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.
1 to 6 of 6
I'm trying to make a simple search engine here, but it turns out it's a bit more difficult than I thought. I've had a look at some tutorials I found, which were all very basic. Most of them were using a SQL query that looks something like SELECT * from articles WHERE keywords LIKE '%$q%', but I guess that won't give me possibilities like "the search result should contain all keywords" and "search result should contain at least one of the keywords", so I tried something else.
This is what I've got so far:
URL could be like this: example.com/?q=keyword1+keyword2 or example.com/?q=keyword1+keyword2&k=all (k=all means the article should contain all keywords instead of just one of them.)
Got a SQL database with table 'articles'.
Columns: id|datestamp|title|keywords|content
Keywords field is filled like this: "keyword1 keyword2 keyword3 etc", keywords seperated with spaces.
$result = mysql_query("SELECT * from articles ORDER BY datestamp")
$articles = mysql_numrows($result);
$i=0;
echo "<ul>\n";
while ($i < $articles) {
$result_keywords = mysql_result($result,$i,"keywords");
$result_title = mysql_result($result,$i,"title");
if ($k) { // Keywords field must contain all keywords
if (keywords field contains all keywords) {
$show_result = "y";
}
} else { // Keywords field must contain at least one of the keywords
if (keywords field contains one of the keywords) {
$show_result = "y";
}
}
if ($show_result == "y") {
echo "<li>$result_title</li>\n";
}
i++;
}
echo "</ul>"; My question is: how can I split the keywords (the ones in the keywords field in the database) and compare them one by one to the ones specified by the user? Any ideas, examples or something?
I don't know how to do exactly what you're going for, but I think your method may become problematic. If you return every field of every article every time somebody runs a search, you'll suck bandwidth like nobody's business.
Here's what I'd do:
Assume you've gotten three keywords from the user: keyword1, keyword2, keyword3. If you're looking for an article that contains all three keywords, use a query like:
SELECT `title` FROM `articles` WHERE INSTR(`keywords`, 'keyword1') > 0 AND INSTR(`keywords`, 'keyword3') > 0 AND INSTR(`keywords`, 'keyword3') > 0;If you're looking for any of the three, change "AND" to "OR". That way you're only returning the attribute you need ("title"), and you're letting the database engine do all of the searching before returning any information.
Like I said, I have no idea if my method is any good. It's just a thought.
I've given your method a try, varland, and it works perfectly! So thank you :-)
Might save me lots of bandwidth indeed.
My god, when I look back at this I'm almost ashamed of myself. Learned a lot since then, heheheh.
1 to 6 of 6