php关联数组和开关 [英] php associative array and switch
问题描述
我的关联数组
$钱=阵列( 镍=> $ _POST [ 镍], Dimes=> $ _POST [硬币],宿舍=> $ _POST [宿舍]);
设置为处理Quarters的html表单,
Dimes和Nickels不会在此显示
< label>< b> Quarters:< / b>< / label>
< option value =。25> 25c< / option>
< option value =。50> 50c< / option>
< option value =。75> 75c< / option>
< option value =1> $ 1.00< / option>
< / select>
用户只能选择Quarters,仅限Dimes或仅限Nickels。
如果用户选择选项值为0.25的Quarters,则该值将被发送到php脚本的
中。
所以我只是想知道基于该事实,即,用户可以选择
小区仅具有一个值,角钱只具有一个值,并且尼克尔斯只能与一个
值为,而不是面值的组合计算,
如何设置不同的测试用例,例如用户选择
$ money [Quarters];
//使用不同的值来自.25,.50,.75或1这样的html表单,并且根据用户选择的内容,只有一个选定的值会通过php脚本。 p>
我可以这样做:
$ p $ switch($ selection)
{
caseQuarters:
echo您选择了$ money ['Quarters']; 。< br />;
休息;
caseNickels:
echo您选择了$ money ['Nickels']; 。< br />;
休息;
caseDimes:
echo您选择了$ money ['Dimes']; 。 < br />;
休息;
默认值:打印请选择一个面额;
}
感谢您不要燃烧newb,我仍然在学习,对不起按照和
。
的形式选定值被提交为 $ _ POST [ '小区']
据我所知,如果用户选择了多个< select>
s(正确?)
所以,我会创建一个这样的支票:
$ selected = 0;
if($ _POST ['quarters']!=DEFAULT_VALUE_OF_YOU_SELECT_QUARTERS)
{
$ selected ++;
}
if($ _POST ['nickels']!=DEFAULT_VALUE_OF_YOU_SELECT_NICKELS)
{
$ selected ++;
}
if($ _POST ['dimes']! =DEFAULT_VALUE_OF_YOU_SELECT_DIMES)
{
$ selected ++;
}
if($ selected> 1)
{
// The用户已选择多个
}
My associative array.
$money = array("Nickels" => $_POST["nickels"], "Dimes" => $_POST["dimes"], "Quarters" =>$_POST["quarters"]);
My html form set up to handle Quarters, Dimes and Nickels are not shown in this case for brevity.
<label><b>Quarters:</b></label>
<select name="quarters" >
<option value=".25">25c</option>
<option value=".50">50c</option>
<option value=".75">75c</option>
<option value="1">$1.00</option>
</select>
A user can only select either Quarters only, Dimes only, or Nickels only. If a user selects Quarters with the option value of .25, this value will be sent to the php script.
So I was just wondering for calculations based on the fact that the user can select Quarters only with one value, Dimes only with one value, and Nickels only with one value, and not a combination of denominations,
how would one go about setting up different test cases, for example if the user selects
$money["Quarters"];
// With different values coming from the html form as .25, .50,.75, or 1, and only one of the selected values will make it through to the php script depending on what the user selected.
Can I do this:
switch($selection)
{
case "Quarters":
echo " You chose $money[‘Quarters’]; .<br />";
break;
case "Nickels":
echo "You chose $money[‘Nickels’]; .<br />";
break;
case "Dimes":
echo "You chose $money[‘Dimes’]; . <br />";
break;
default: print "Please select a Denomination";
}
Thank you for not flaming the newb, I am still learning, and sorry for the mix and match in terms of "
and "
.
Selected values in a form are submitted as $_POST['quarters']
.
I understand, that you want to check, if the user has selected more than one of your <select>
s (correct?)
So, I'd create a check like this:
$selected = 0;
if ($_POST['quarters'] != "DEFAULT_VALUE_OF_YOU_SELECT_QUARTERS")
{
$selected++;
}
if ($_POST['nickels'] != "DEFAULT_VALUE_OF_YOU_SELECT_NICKELS")
{
$selected++;
}
if ($_POST['dimes'] != "DEFAULT_VALUE_OF_YOU_SELECT_DIMES")
{
$selected++;
}
if ($selected > 1)
{
// The user has selected more than one
}
这篇关于php关联数组和开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!