Forum Moderators: open

Message Too Old, No Replies

Multiple Keywords in like

         

switchjohnny

8:26 am on Oct 31, 2007 (gmt 0)

10+ Year Member



I want to do this.

SELECT * FROM table WHERE field like ('%var%' AND '%var2%');

Actually, I want to do this for more than 2 vars.

I want a user to be able to select many options from a list of checkboxes and get results from those selections.

Can this be done with an array of some kind?

rocknbil

6:08 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<input type="checkbox" name="var1" id="var1" value="One"> One
<input type="checkbox" name="var2" id="var2" value="Two"> Two
<input type="checkbox" name="var3" id="var3" value="Three"> Three
<input type="checkbox" name="var3" id="var4" value="Four"> Four

A perl-y or PHP-like example is as follows, use the language of your choice:

## In this example, incoming values are stored in %qs
## If a box is not CHECKED, it will not be in the incoming vars
## so simply check for it's existence. @list is the
## array/list of the checkboxes.


@list = ('var1','var2','var3','var4');
foreach $var (@list) {
if ($qs{$var}) {
## you only need to "and" if $where has a value
if ($where) { $where .= ' and'; }
$where .= " field like '%$qs{$var}%'";
}
}
$select = "select * from table";
if ($where) { $select .= " where ($where);"; }

So if 1 and 3 are checked what you will have is

select * from table where (field like '%One%' and field like '%Three%');

Although it may be possible you are looking for OR, not AND. AND will only match on records meeting ALL conditions, or will match on ANY of the conditions.

switchjohnny

9:03 pm on Oct 31, 2007 (gmt 0)

10+ Year Member



Thanks, this makes sense, I'm using PHP, so some of the coding is different. I'm still trying to figure it out, but this is a good start.