Something at the top - :)

-----Original Message-----
From: David Robley [mailto:[EMAIL PROTECTED]]
Sent: Friday, February 15, 2002 2:47 PM
Subject: RE: [PHP] searching key words from a database field

In article <[EMAIL PROTECTED]>, 
> 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:[EMAIL PROTECTED]]
> Sent: Friday, February 15, 2002 11:27 AM
> To: David Robley
> Cc: David Robley; [EMAIL PROTECTED]; 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" <[EMAIL PROTECTED]>
> Sent: Friday, February 15, 2002 1:19 PM
> Subject: Re: [PHP] searching key words from a database field
> > In article <000801c1b5b0$ee66f180$0401a8c0@philip>,
> > [EMAIL PROTECTED] says...
> > > This works for the loop.  now spliting up the words.  now it searchs
> > > 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 

//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 

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!!


Reply via email to