我有以下html:
<form action="" method="get">
<ul>
<li>location
<select name="locationdo">
<option value="tax_cb">Checkbox</option>
<option value="tax_radio">Radio</option>
<option value="tax_dd">Dropdown</option>
</select>
</li>
<li>genre
<select name="genredo">
<option value="tax_cb">Checkbox</option>
<option value="tax_radio">Radio</option>
<option value="tax_dd">Dropdown</option>
</select>
</li>
<li>studio
<select name="studiodo">
<option value="tax_cb">Checkbox</option>
<option value="tax_radio">Radio</option>
<option value="tax_dd">Dropdown</option>
</select>
</li>
</ul>
<p><input type="submit" value="submit" name="submit" /></p>
</form>
我想要的是根据所做的选择将$which_tax_array
中的数组元素存储在单独的变量中。希望代码能够更好地解释我想要实现的目标(但它不能像我想要的那样工作):
if (isset($_GET['submit'])) {
$which_tax_array = array('location', 'genre', 'studio');
$what = array();
foreach ($which_tax_array as $key => $tax_name) {
$what[$tax_name] = $_GET[$tax_name.'do'];
foreach ($what as $tax_term => $display_option) {
if ( in_array($what[$tax_name], $what) ) {
$checkboxes = ','.$tax_term;
} elseif ( in_array($what[$tax_name], $what) ) {
$radios .= ','.$tax_term;;
} elseif ( in_array($what[$tax_name], $what) ) {
$dropdowns .= ','.$tax_term;
}
}
}
}
echo 'cb '.$checkboxes.'<br>';
echo 'radio '.$radios . '<br>';
echo 'dd '.$dropdowns.'<br>';
答案 0 :(得分:0)
不确定你要做什么。我看到一些可能的问题 -
(1)你没有关闭foreach ($which_tax_array as $key => $tax_name)
直到结束,所以你太早执行你的下一个foreach
(2)您有$checkboxes = ','.$tax_term;
而不是$checkboxes .= ','.$tax_term;
,因此您覆盖了$checkboxes
而不是附加。
(3)当您检查数组值是否在其自己的数组中时,您的if ( in_array($what[$tax_name], $what) )
将会成立,因此您的所有值都将在$checkboxes
中。我想您要检查值是否等于tax_cb
,tax_radio
或tax_dd
。
尝试这样的事情 -
if (isset($_GET['submit'])) {
$which_tax_array = array('location', 'genre', 'studio');
$what = array();
foreach ($which_tax_array as $key => $tax_name) {
$what[$tax_name] = $_GET[$tax_name.'do'];
}
foreach ($what as $tax_term => $display_option) {
if ($what[$tax_name] == 'tax_cb') {
$checkboxes .= ','.$tax_term;
} elseif ($what[$tax_name] == 'tax_radio') ) {
$radios .= ','.$tax_term;;
} elseif ($what[$tax_name] == 'tax_dd') ) {
$dropdowns .= ','.$tax_term;
}
}
echo 'cb '.$checkboxes.'<br>';
echo 'radio '.$radios . '<br>';
echo 'dd '.$dropdowns.'<br>';
}