adwfun,
No I don't mean code for each possible combination.
Since Drop down boxes have only 1 "answer" dispite the fact they can have a HUGE list.
What I recommended is this; if you have 106 drop boxes (for 106 columns for instance) you would have to do the if statement 106 times.
You don't have to use 0 for the variable.. you can do it like this..
<select name="test_box">
<option value="XXXX" selected="selected">Select</option>
<option>Option One</option>
<!-- or you can use -->
<option value="1">Option 1</option>
<!-- .... -->
</select>
And the option labelled "Select" will carry the value of XXXX, if it is XXXX, then you won't have to insert that into the query.
What the script does is this; it starts with a base query:
$query = "SELECT * from table WHERE ";
This is the static portion of the query, it doesn't need to change based on the options the user has selected.
if ($_POST['value1'] != 0) {
$query = $query."field1 = '$s1'," ;
}
This is just an example, it is of course vulnerable to SQL injection, but I am making it simple to understand the process.
Try This (below):
<!-- Test.html-->
<body>
<form id="optFrm" name="optFrm" method="post" action="optform.php">
<p>
<select name="op1">
<option value="XXXX" selected="selected">Select</option>
<option>Howdie Doodie</option>
<option>Special Day</option>
<option>big.nerd rocks</option>
</select>
</p>
<p>
<select name="op2">
<option value="XXXX" selected="selected">Select</option>
<option>Howdie Doodie</option>
<option>Special Day</option>
<option>big.nerd rocks</option>
</select>
</p>
<p>
<select name="op3">
<option value="XXXX" selected="selected">Select</option>
<option>Howdie Doodie</option>
<option>Special Day</option>
<option>big.nerd rocks</option>
</select>
</p>
<p>
<select name="op4">
<option value="XXXX" selected="selected">Select</option>
<option>Howdie Doodie</option>
<option>Special Day</option>
<option>big.nerd rocks</option>
</select>
</p>
<p>
<select name="op5">
<option value="XXXX" selected="selected">Select</option>
<option>Howdie Doodie</option>
<option>Special Day</option>
<option>big.nerd rocks</option>
</select>
</p>
<p>
<select name="op6">
<option value="XXXX" selected="selected">Select</option>
<option>Howdie Doodie</option>
<option>Special Day</option>
<option>big.nerd rocks</option>
</select>
</p>
</form>
</body>
Then the code for: optform.php
$op1 = $_POST['op1'];
$op2 = $_POST['op2'];
$op3 = $_POST['op3'];
$op4 = $_POST['op4'];
$op5 = $_POST['op5'];
$op6 = $_POST['op6'];
$found = 0;
$query = "SELECT * from table WHERE ";
if ($op1 != "XXXX") {
$query = $query."field1 = '$op1'," ;
$found++;
}
if ($op2 != "XXXX") {
$query = $query."field1 = '$op1'," ;
$found++;
}
if ($op3 != "XXXX") {
$query = $query."field1 = '$op1'," ;
$found++;
}
if ($op4 != "XXXX") {
$query = $query."field1 = '$op1'," ;
$found++;
}
if ($op5 != "XXXX") {
$query = $query."field1 = '$op1'," ;
$found++;
}
if ($op6 != "XXXX") {
$query = $query."field1 = '$op1'," ;
$found++;
}
if ($found > 0) {
$query = substr_replace($query,strlen($query),'',-1);
echo "The Query is: $query";
} else {
echo "You left all of them at select! How Come??";
}
Try using different changes in the Drop Down Boxes, and see how it changes the query.
big.nerd