PCHomepage
Programmer
I created a relatively simply function that gets its input from another select box. There are actualy six of each in two rows making twelve select boxes all together that are used for building up a dynamic query but I'm have a little trouble figuring out the logic. They are working togeother but not as needed.
In the blue lines below, I am trying to retrieve the values being submitted by the previous select box so that I can have the next one bring up only valid results but when I select one, no values appear in any.
In the red area, I am trying to create an array of values from multiple choices but the choices are not numeric and I can't seen to get then enquoted even though I have a function for doing so. When I try, it gives an array of all values including the empty ones. It is the array that is to be used in the next select box to know what to choose but as it exists only in the previous select box, I am not sure how to pass it on from select box to select box AND accumulate the array values from one to the next so that each successive select box fetches only valid entries. Only the Value2 and forward select boxes need the information
Also, the boxes need to auto-submit AND allow multiple choices but it seems to remember only one choice. Any ideas?
The function input variables are:
[bold]$NameVal[/bold] is the name of the select box, ie: Value1, Value2 etc.
[bold]$DefaultVal[/bold] is what shows at the top of the list box as a label, ie "Value 1", "Value 2" etc.
[bold]$SelectVal[/bold] is the value coming into it from the select box above it that tells it what data to select
In the blue lines below, I am trying to retrieve the values being submitted by the previous select box so that I can have the next one bring up only valid results but when I select one, no values appear in any.
In the red area, I am trying to create an array of values from multiple choices but the choices are not numeric and I can't seen to get then enquoted even though I have a function for doing so. When I try, it gives an array of all values including the empty ones. It is the array that is to be used in the next select box to know what to choose but as it exists only in the previous select box, I am not sure how to pass it on from select box to select box AND accumulate the array values from one to the next so that each successive select box fetches only valid entries. Only the Value2 and forward select boxes need the information
Also, the boxes need to auto-submit AND allow multiple choices but it seems to remember only one choice. Any ideas?
The function input variables are:
[bold]$NameVal[/bold] is the name of the select box, ie: Value1, Value2 etc.
[bold]$DefaultVal[/bold] is what shows at the top of the list box as a label, ie "Value 1", "Value 2" etc.
[bold]$SelectVal[/bold] is the value coming into it from the select box above it that tells it what data to select
Code:
function Value($NameVal, $DefaultVal, $SelectVal, $mysqli) {
global $Selected;
global $Where;
[COLOR=blue]// Create variables from posts
$Value1 = ($NameVal != "Value1") ? $_POST['Value1'][0]: $Value1 = "";
$Value2 = ($NameVal != "Value2") ? $_POST['Value2'][0]: $Value2 = "";
$Value3 = ($NameVal != "Value3") ? $_POST['Value3'][0]: $Value3 = "";
$Value4 = ($NameVal != "Value4") ? $_POST['Value4'][0]: $Value4 = "";
$Value5 = ($NameVal != "Value5") ? $_POST['Value5'][0]: $Value5 = "";
$Value6 = ($NameVal != "Value6") ? $_POST['Value6'][0]: $Value6 = "";
$Values = array($Value1, $Value2, $Value3, $Value4, $Value5, $Value6);[/color]
[COLOR=red]// Build array of values already selected
if (is_array($Values) && count(array_filter($Values)) > 0):
$Value = array_filter($Values);
$AndID = " (";
$AndID .= implode(",", $Value);
$AndID .= ") ";
endif;[/color]
if ($SelectVal) $Where = " WHERE KeyID = ".$SelectVal;
if (!$SelectVal) $Where = " WHERE KeyID = IS NULL"; // Keeps select box depopulated until needed
if ($AndID) $Where .= " AND QEValue IN ".$AndID;
$Query = " SELECT DISTINCT Value, ID FROM keys ".$Where." GROUP BY Value ORDER BY Value";
$Multiple = " multiple=\"multiple\"";
$NumRows = " size=\"5\"";
$AddRow = "<option value=\"\"></option>\n";
$OnChange = " onchange='this.form.submit()'";
$Output = "<select name=\"".$NameVal."[]\" id=\"".$NameVal."[]\" class=\"SelectBox\"".$OnChange.$Multiple.$NumRows.">\n";
$Output .= "<option value=\"\">".$DefaultVal."</option>\n";
if ($Where):
if ($result = $mysqli->query($Query)):
$i = 0;
while ($row = $result->fetch_row()):
$Selected = ($row[0] == $_POST[$NameVal][0]) ? $Selected = " SELECTED" : $Selected = "";
$Output .= "<option value=\"".$row[0]."\"".$Selected.">".$row[0]."</option>\n";
$i++;
endwhile;
$result->close();
endif;
endif;
$Output .= "</select>";
echo $Output;
}