Skip to main content
Participant
May 4, 2008
Question

Search form with checkbox array - how to build the query PHP CS3

  • May 4, 2008
  • 5 replies
  • 465 views
I have a search form, search.php with which I have successfully returned the correct results from a single checkbox and a single textfield on results.php. Now I want to add a checkbox group that produces an array, so I use name="checkbox[]" for each item in the checkbox group. My question is, how do I construct the Record Set, specifically, what should I use in the SELECT statement and how do I construct the variable? Thank you in advance.
This topic has been closed for replies.

5 replies

Inspiring
May 6, 2008
On Mon, 5 May 2008 18:51:03 +0000 (UTC), "aquas"
<webforumsuser@macromedia.com> wrote:

>The value is non-numeric. Do I have to write something different, then?
>
>Thank you for taking the time with me.

You're welcome. Small change.

Change this:

if(is_array($_POST['bedrooms'])){
$bedrooms=join(',',$_POST['bedrooms']);
$sql.=" WHERE bedrooms IN ($bedrooms)";
}

To this:

if(is_array($_POST['bedrooms'])){
$bedrooms="'".join("','",$_POST['bedrooms'])."'";
$sql.=" WHERE bedrooms IN ($bedrooms)";
}

Gary
aquasAuthor
Participant
May 5, 2008
The value is non-numeric. Do I have to write something different, then?

Thank you for taking the time with me.
Inspiring
May 5, 2008
On Sun, 4 May 2008 21:37:19 +0000 (UTC), "aquas"
<webforumsuser@macromedia.com> wrote:

>The table I'm searching is apartment listings and the checkboxes are bedrooms.
>So if a user wants to see 1 bedroom and 2 bedroom apartments the results should
>show only 1 and 2 bedroom apartments. If nothing is checked then it should show
>all the bedroom options (I guess that's the same as checking all of them). All
>the values are found in the same field.

Is the bedrooms field in the database numeric or character? Consider
this example which assumes the field is numeric:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input type="checkbox" name="bedrooms[]" value="1" /> 1<br />
<input type="checkbox" name="bedrooms[]" value="2" /> 2<br />
<input type="checkbox" name="bedrooms[]" value="3" /> 3<br />
<input type="checkbox" name="bedrooms[]" value="4" /> 4<br />
<input type="submit" />
</form>
<?php
$sql='SELECT * FROM apartments';
if(is_array($_POST['bedrooms'])){
$bedrooms=join(',',$_POST['bedrooms']);
$sql.=" WHERE bedrooms IN ($bedrooms)";
}
print "<p>$sql</p>\n";
?>
</body>
</html>

Gary
aquasAuthor
Participant
May 4, 2008
The table I'm searching is apartment listings and the checkboxes are bedrooms. So if a user wants to see 1 bedroom and 2 bedroom apartments the results should show only 1 and 2 bedroom apartments. If nothing is checked then it should show all the bedroom options (I guess that's the same as checking all of them). All the values are found in the same field.

Thank you for your reply and I hope I have given you enough to go on.
Inspiring
May 4, 2008
On Sun, 4 May 2008 19:13:38 +0000 (UTC), "aquas"
<webforumsuser@macromedia.com> wrote:

>I have a search form, search.php with which I have successfully returned the
>correct results from a single checkbox and a single textfield on results.php.
>Now I want to add a checkbox group that produces an array, so I use
>name="checkbox[]" for each item in the checkbox group. My question is, how do I
>construct the Record Set, specifically, what should I use in the SELECT
>statement and how do I construct the variable? Thank you in advance.

It depends on what you want to accomplish. It's impossible to know with
the information you gave. For example, are you looking for any of the
checked values, or all of the checked values. Are all the values
supplied by the checkboxes going to be found in the same field?

Gary