Not signed in (Sign In)

SkillShare - A place to discuss Web Standards and Web Design topics

Categories

Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthorverb
    • CommentTimeJan 8th 2007 edited
     permalink

    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?

  1.  permalink
    You could explode the string and compare them one at a time, if that's what you meant.
    • CommentAuthorvarland
    • CommentTimeJan 8th 2007
     permalink

    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.

    • CommentAuthorverb
    • CommentTimeJan 8th 2007
     permalink

    I've given your method a try, varland, and it works perfectly! So thank you :-)
    Might save me lots of bandwidth indeed.

    • CommentAuthordavist11
    • CommentTimeJan 8th 2007
     permalink
    You could always do a fulltext search
    • CommentAuthorverb
    • CommentTimeSep 14th 2007
     permalink

    My god, when I look back at this I'm almost ashamed of myself. Learned a lot since then, heheheh.

Add your comments
    Username Password
  • Format comments as (Help)