Hiya,

I'm writing a bit of code to add a bit of extra functionality to a basic search on a 
web site.

When searching for items in a library, I want to be able to select what I want and 
don't want to be searched for, i.e. +linux -beginner searches for all books on Linux, 
but doesn't show any for beginners.

I've got the code to generate the SQL statement:

$commands = explode(" ", $searchstring);
$sql = "SELECT * FROM ".$searchtable." WHERE ";

for ($i=0;$i<count($commands);$i++) {
    if (preg_match("/^([\+\-]?)(\S+)$/", $commands[$i], $preg)) {
        if ($i > 0) {
            switch($preg[1]) {
                case "+":
                    $sql .= " AND ";
                    break;

                case "-":
                    $sql .= " AND NOT ";
                    break;

                default:
                    $sql .= " OR ";
                    break;
            }
        }

        if ($i == 0 && $preg[1] == "-") {
            $sql .= " NOT ";
        }

        $search = array("/%/", "/_/", "/\*/", "/\?/");
        $replace = array("\\%", "\\_", "%", "_");
        $preg[2] = preg_replace($search, $replace, $preg[2], -1);

        $sql .= "title LIKE '%".$preg[2]."%'";
    }
}

Which turns '+php -perl' into:

SELECT * FROM books WHERE title LIKE '%php%' AND NOT title LIKE '%perl%' 

The problem I get is that the query doesn't seam to work. One of the books in the 
table is 'Professional PHP Programming', but, while 'php' will pick it up, and 'php 
perl' as well, 'php -perl' won't!

Surly it should work! :-S

Thanks for your help,

--
Jonathan Wright
[EMAIL PROTECTED]
[EMAIL PROTECTED]
--


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to