0
Search form with checkbox array - how to build the query PHP CS3
New Here
,
/t5/dreamweaver-discussions/search-form-with-checkbox-array-how-to-build-the-query-php-cs3/td-p/959403
May 04, 2008
May 04, 2008
Copy link to clipboard
Copied
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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/dreamweaver-discussions/search-form-with-checkbox-array-how-to-build-the-query-php-cs3/m-p/959404#M197237
May 04, 2008
May 04, 2008
Copy link to clipboard
Copied
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
<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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
aquas
AUTHOR
New Here
,
/t5/dreamweaver-discussions/search-form-with-checkbox-array-how-to-build-the-query-php-cs3/m-p/959405#M197238
May 04, 2008
May 04, 2008
Copy link to clipboard
Copied
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.
Thank you for your reply and I hope I have given you enough to go on.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/dreamweaver-discussions/search-form-with-checkbox-array-how-to-build-the-query-php-cs3/m-p/959406#M197239
May 05, 2008
May 05, 2008
Copy link to clipboard
Copied
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
<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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
aquas
AUTHOR
New Here
,
/t5/dreamweaver-discussions/search-form-with-checkbox-array-how-to-build-the-query-php-cs3/m-p/959407#M197240
May 05, 2008
May 05, 2008
Copy link to clipboard
Copied
The value is non-numeric. Do I have to write something
different, then?
Thank you for taking the time with me.
Thank you for taking the time with me.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
LATEST
/t5/dreamweaver-discussions/search-form-with-checkbox-array-how-to-build-the-query-php-cs3/m-p/959408#M197241
May 06, 2008
May 06, 2008
Copy link to clipboard
Copied
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
<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
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

