Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

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

New Here ,
May 04, 2008 May 04, 2008
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.
TOPICS
Server side applications
440
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 04, 2008 May 04, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 04, 2008 May 04, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 05, 2008 May 05, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 05, 2008 May 05, 2008
The value is non-numeric. Do I have to write something different, then?

Thank you for taking the time with me.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 06, 2008 May 06, 2008
LATEST
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines