RE: [PHP] searching key words from a database field
| From: | Martin Towell | Date: | Fri, 15 Feb 2002 03:58:20 +0000 |
| Subject: | RE: [PHP] searching key words from a database field | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-84858@lists.php.net to get a copy of this message | ||
Something at the top - :)
-----Original Message-----
From: David Robley [mailto:robleyd@paradise.net.nz]
Sent: Friday, February 15, 2002 2:47 PM
To: php-general@lists.php.net
Subject: RE: [PHP] searching key words from a database field
In article <6416776FCC55D511BC4E0090274EFEF5089F22@exchange.world.net>,
martin.towell@world.net says...
> try something like this (not tested):
>
> <?
> $getme = "the big tree";
> $getme_arr = explode(" ", $getme);
> $num = count($getme_arr);
>
> $sql = "SELECT * FROM
hyperlinks WHERE 1";
>
> for ($i = 0; $i < $num; $i++)
> if (strlen($getme_arr[$i]) > 0)
> $sql .= " AND keywords LIKE
> '%".$getme_arr[$i]."%'";
>
> $sql .= " ORDER BY id ASC LIMIT 0, 30";
> ?>
>
> -----Original Message-----
> From: Philip J. Newman [mailto:newmanpj@philipsdomain.com]
> Sent: Friday, February 15, 2002 11:27 AM
> To: David Robley
> Cc: David Robley; php-general@lists.php.net; Gareth Hawken
> Subject: Re: [PHP] searching key words from a database field
>
>
> the problem is there isn't any code yet
>
> and yes what you said is corrent
>
> if text input is: the big tree
>
> would like $string1 = the
> would like $string2 = big
> would like $string3 = tree
>
> and so on and so on for all the key words entered. Each key word needs to
> have its own string alocated. Any suggestions.
>
> ps: nice to see another kiwi in here.
>
> ----- Original Message -----
> From: "David Robley" <robleyd@paradise.net.nz>
> To: <php-general@lists.php.net>
> Sent: Friday, February 15, 2002 1:19 PM
> Subject: Re: [PHP] searching key words from a database field
>
>
> > In article <000801c1b5b0$ee66f180$0401a8c0@philip>,
> > newmanpj@philipsdomain.com says...
> > > This works for the loop. now spliting up the words. now it searchs
for
> > > just one word in the loop
Guys - can you set your mailer/news thingy to doofollowups at the end of
the article, please?
Now, let's take Martin's concept a little further. Suppose you want to
have an option that goes something like
"search for ALL words; search for ANY word" You might also want 'search
for exact phrase' but the implementation of that is dead easy so I'll
leave that for you :-)
So this choice might be made via checkbutton, dropdown or whatever, but
it returns the value AND or OR for ALL words or ANY words respectively.
Or it can return anything you like, and you test and use AND or OR as
required.
<?
//ALL or ANY?? Assume passed in as $opval and is either AND or OR
$getme = "the big tree";
// You might want to do something here if $getme is empty, or has only
// one word in it??
$sql = "SELECT * FROM hyperlinks";
if(!empty($getme)) {
$getme_arr = explode(" ", $getme);
$num = count($getme_arr);
if(1 == $num) { //Only one word to search on
$sql.= " WHERE keywords LIKE '$getme_arr' ";
} else {
$sql.= " WHERE keywords LIKE '$getme_arr[0]' ";
for ($i = 1; $i < $num; $i++)
if (strlen($getme_arr[$i]) > 0)
$sql .= " $opval keywords LIKE '%".$getme_arr[$i]."%'";
}
$sql .= " ORDER BY id ASC LIMIT 0, 30";
?>
Hmm, getting a bit rusty; there are probably better/neater ways of doing
that. But it should give you something to chew on. And I am sure I'm
missing a } or three in there but you do have to do some of the work :-)
And Philip, sorry, I'm not a real Kiwi - just here for a year or so from
Oz.
--
David Robley
Temporary Kiwi!
--
database search logic like this can get _REALLY_ complex - you could
1) combine phrase and word search
eg something "some text together"
2) allow the user to decide which logic function to use
eg foo and bar or hello not world
3) and combine the two
eg foo and "bar lala" or "hello world" not anything
I'll let you figure out how to do all that - I've done it once, lost the
code and now have too many white hairs to think about doing it again!!
Martin
(Not-a-Kiwi)