SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
Thread: Checkbox array/looping SQL query
-
Sep 24, 2007, 21:15 #1
- Join Date
- Aug 2004
- Location
- San Clemente, CA
- Posts
- 859
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Checkbox array/looping SQL query
Hello, I have a form with multiple checkbox's and if a user selects the box, a dropdown menu appears. Based on what options they pick, I want to create an array of their selections and assemble a SQL query (where $x=1 AND $y=2 AND $z=3, etc..).
Here is my form which have 3 drop down menus:
HTML Code:<form name="form1" method="post" action=""> Difficulty: <input type="checkbox" onclick="document.getElementById('difficulty').disabled=!this.checked" /> <select disabled="disabled" id="difficulty"> <option value="hard">Hard</option> <option value="easy">Easy</option> </select> <br> Direction: <input type="checkbox" onclick="document.getElementById('direction').disabled=!this.checked" /> <select disabled="disabled" id="direction"> <option value="left">Left</option> <option value="right">Right</option> </select> <br> Color: <input type="checkbox" onclick="document.getElementById('color').disabled=!this.checked" /> <select disabled="disabled" id="color"> <option value="left">Red</option> <option value="right">Green</option> </select> </form>
Pending on which variables are selected in the checkboxs above, it should create an array to be assembled to query the database. If all three variables were selected from the focm above, my SQL would look like this:
PHP Code:SELECT * FROM resort WHERE direction='left' AND difficulty='easy' AND color='red'
PHP Code:<?php
$arr = $_POST['value'];
$id = $_POST['id'];
$select="SELECT * FROM resort";
$where="";
foreach ($arr as &$value)
{
echo "WHERE $id='$value' AND";
}
?>
-
Sep 24, 2007, 21:20 #2
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
First, give your form elements names, or you won't be able to receive the values in PHP.
PHP Code:$sql = "SELECT * FROM resort WHERE 1=1";
if (!empty($_GET['difficulty'])) {
$sql .= " AND difficulty = '" . mysql_real_escape_string($_GET['difficulty']) . "'";
}
if (!empty($_GET['direction'])) {
$sql .= " AND direction = '" . mysql_real_escape_string($_GET['direction']) . "'";
}
if (!empty($_GET['color'])) {
$sql .= " AND color = '" . mysql_real_escape_string($_GET['color']) . "'";
}
Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
Sep 24, 2007, 21:39 #3
- Join Date
- Aug 2004
- Location
- San Clemente, CA
- Posts
- 859
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks Dan, that works wonderfully!
-
Sep 25, 2007, 07:02 #4
I would do something like this just to reduce code repeat.
PHP Code:$sql = 'SELECT * FROM resort WHERE 1=1';
$fields = array('difficulty', 'direction', 'color');
foreach ($fields as $field) {
if (!isset($_GET[$field]) || empty($_GET[$field])) {
continue;
}
$_GET[$field] = mysql_real_escape_string($_GET[$field])
$sql .= sprintf(" AND %s = '$s'", $field, $_GET[$field]);
}
-
Sep 25, 2007, 13:31 #5
- Join Date
- Aug 2004
- Location
- San Clemente, CA
- Posts
- 859
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thank you very much I will check it out
Bookmarks