Forum Moderators: coopster
EX:
Selection 1 checked
Selection 2
Selection 3 checked
OUTPUT
Selection 1
Selection 3
I am kinda lost on how to form the sql query. Am I going to have to make an if statment for each possiblity? Or is there a way for me to set one query that will only pull the active selections?
$fields = "";
if ($selection1 == "yes")
{
$fields .= "selection1,";
}
if ($selection2 == "yes")
{
$fields .= "selection2,";
}
if ($selection3 == "yes")
{
$fields .= "selection3";
}
$query = "select " . $fields . " from table where somefield=somevalue";
mysql_query($query);
etc..is that what you mean? I didn't add the check for proper commas between the fields but you get the idea.
I was thinking if(fields=1,2,3){ query
}ifelse (fields=1,2) {query} and so forth. (note that is not correct coding) Your way will still be long because there will be probably 25 selections but much easier. Thank you again!
your form
<form action="result.php" method="post" enctype="multipart/form-data">
<input type="checkbox" name="Select1" value="field1" />Select 1<br />
<input type="checkbox" name="Select2" value="field2" />Select 2<br />
<input type="checkbox" name="Select3" value="field3" />Select 3<br/>
<input type="submit" name="Submit" id="Submit" value="Submit"><input type="reset" name="Reset" id="Reset" value="Reset">
result.php file
$fields = "";
if (isset($Select1))
{
$fields .= $Select1 . ",";
}
if (isset($Select2))
{
$fields .= $Select2 . ",";
}
if (isset($Select3))
{
$fields .= $Select3;
}
if (empty($fields))
{
$query = "no fields selected";
} else {
$query = "select " . $fields . " from table where somefield=somevalue";
}
echo $query;
knowles, if you always add the comma then you cut it every time it makes it more consistent and easier to handle. Otherwise you have to add a test to see if its the last field and then not cut the comma. Might as well just do it every time, easy.
Form:
<form method="post" action="makequery.php">
First: <input type="checkbox" name="one" value="first"><br>
Second: <input type="checkbox" name="two" value="second"><br>
Third: <input type="checkbox" name="three" value="third"><br>
<input type="submit">
</form>
$selected = implode(",", $HTTP_POST_VARS);
echo $selected;
It forms a string ($selected) with comma delimited values.
Test it using the in_array [php.net] function.
The thing about the comma - this is a good trick to know, because the situation comes up a lot. It's easiest to add a comma after every field so you know it's always going to be there, then you always trim it:
$fields = substr($fields, -1);
This just trims off the last character and you can use this technique to simplify putting together WHERE conditions or any number of other things in addition to the situation where you are using it. It's simple and failsafe.
Tom
Here are two methods for extracting the checked values from a form containing other input types:
('four' and 'five' are hypothetical names of two non-checkbox fields)
$selected = $HTTP_POST_VARS;
unset($selected[four]);
unset($selected[five]);
$qstr = implode(",", $selected);
echo $qstr;
$nix = array("four","five");
while (list($key, $val) = each($HTTP_POST_VARS))
{
if(!in_array($key, $nix) ){
$selected .= $key . ",";
}
}
$qstr = substr($selected,0,-1);
echo $qstr;
I was following everything up until the impode thing
From the PHP manual:
implode
(PHP 3, PHP 4 >= 4.0.0)
implode -- Join array elements with a string
Description
string implode ( string glue, array pieces)
Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element.
Example 1. implode() example
$colon_separated = implode(":", $array);
So if the array has
$array[0] = "elem1"
$array[1] = "elem2"
implode(",", array);
yields the string: "elem1, elem2".
Of course, wathc out for this particularly if you are sending data via GET - any idiot could change the params and, in your case, select any columns he wants as long as he can figure out the names.
Tom