Mailing List Archive



Back to the month index Back to the list index

Gary E. Bickford (garyb@fxt.com)
Tue, 15 Apr 1997 10:20:15 -0600


Message-Id: <v03020906af79507a3b5b@[163.185.20.250]>
Date: Tue, 15 Apr 1997 10:20:15 -0600
From: "Gary E. Bickford" <garyb@fxt.com>
Subject: Re: [mSQL] Boolean Searches with w3-msql??

"G.h.van den Berg" <gvandenb@europe.poppe.com> opined,

>Hi there,
>I was just wondering if anyone had found a way of doing AND or OR searches
>from a web page.
>
>Basically i have an input box into which users can enter term1 AND term2 or
>term1 OR term2 ..

Sure. I use PHP, but you can do the equivalent in any of the other
languages supporting msql. Two basic ways:

1. Provide two separate text boxes, with a radio button in between.
Construct the query based on the input. This may be the easiest for you
and the user, for simple queries.

2. Parse a single input of the form "jones or smith & diggons", or
"jones smith diggons" with an implied OR. In PHP (coding out of my head,
and untested), something like this would work:
        <?
        reset ($tokens);
        $ntok=0; /* just so we know how many */
        $tok=strtok($string," ");
        while ($tok) {
                $ntok++;
                $tokens[]=$tok;
                $tok=strtok(" ");
        }
        /* Now we have an array $tokens that contains all the items in the
input separated by spaces. Go through the array, building a search string.
I'm ignoring error checking for this time. There are some tricky things
that must be done to parse correctly, given man's tendency to provide
incorrectly formed input. */
        if ($ntok) {
                $search="SELECT foo FROM bar WHERE ";
                reset ($tokens);
                $next_token=0;
                $state="term";
/* Assuming perfectly formed input */
                while ($next_token < $ntok) {
/* See if it's either "and" or "or" or "&" or "|" ... */
/* This is not particularly elegantly or efficiently coded - whataya want
for my coffee break? :) */
                        $tok=$token[]; /* Retain case for actual search
term */
                        $op=strtolower($tok);
                        if ($op == "and" || $op == "&") {
                                if ($state="op" {
                                        $search += "AND ";
                                        $state="term";
                                } else { echo "parse error"; }
                        }
                        elseif ($op == "or" || $op == "|") {
                                if ($state="op" {
                                        $search += "OR ";
                                        $state="term";
                                } else { echo "parse error"; }
                        } elseif (state="term") {
                                $search += "foo LIKE '%$op%' ";
                                $state="op";
                        } else { /* state == "op" */
/* adjacent terms are OR'd */
                                $search += " OR foo LIKE '%$tok%' ";
                        }
/* add a bunch of } here. I've got work to do. */
/* PS - if $state is "term" at the end, the string ends in AND or OR or
something. Deal with it. */

>

There y'ar. A quick and pretty funky, untested state-based parser for
basic unnested input logic (no paren's, left to eight precedence). A more
appropriate way in classic programming is a recursive function call, but
that's a little much for PHP.

GB

--------------------------------------------------------------------------
To remove yourself from the Mini SQL mailing list send a message containing
"unsubscribe" to "unsubscribe" to msql-list-request@bunyip.com. Send a message containing
"info msql-list" to majordomo@bunyip.com for info on monthly archives of
the list. For more help, mail owner-msql-list@bunyip.com NOT the msql-list!