RE: [PHP] searching key words from a database field
| From: | David Robley | Date: | Fri, 15 Feb 2002 03:46:30 +0000 |
| Subject: | RE: [PHP] searching key words from a database field | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-84857@lists.php.net to get a copy of this message | ||
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!