在 PHP 页面上运行多个 Else If 语句
我想知道在一个 PHP 页面上运行两个(或更多) else if 语句的最佳方法是什么。这是表格的一部分,用于查找门票类型和天数。我需要在一页上运行其中两个语句,但我似乎不起作用,因为一旦它遇到真正的语句,它就会停止。我怎样才能让它发挥作用?
<?php
if($ticket_type=='Child (5 & Under)'){
echo "
<select name=\"ticket_type[]\">
<option value=\"\">Select one...</option>
<option selected=\"selected\">Child (5 & Under)</option>
<option>Child (6 - 12)</option>
<option>Teen (13 - 17)</option>
</select>";
}else if($ticket_type=='Child (6 - 12)'){
echo "
<select name=\"ticket_type[]\">
<option value=\"\">Select one...</option>
<option>Child (5 & Under)</option>
<option selected=\"selected\">Child (6 - 12)</option>
<option>Teen (13 - 17)</option>
</select>";
}else if($ticket_type=='Teen (13 - 17)'){
echo "
<select name=\"ticket_type[]\">
<option value=\"\">Select one...</option>
<option>Child (5 & Under)</option>
<option>Child (6 - 12)</option>
<option selected=\"selected\">Teen (13 - 17)</option>
</select>";
}else{
echo "
<select name=\"ticket_type[]\">
<option value=\"\">Select one...</option>
<option>Child (5 & Under)</option>
<option>Child (6 - 12)</option>
<option>Teen (13 - 17)</option>
</select>";
}?>
<?php
if($days=='1 Day'){
echo "
<select name=\"ticket_type[]\">
<option value="">Select one...</option>
<option selected=\"selected\">1 Day</option>
<option>2 Days</option>
<option>3 out of 4 Days</option>
</select>";
}else if($days=='2 Days'){
echo "
<select name=\"ticket_type[]\">
<option value="">Select one...</option>
<option>1 Day</option>
<option selected=\"selected\">2 Days</option>
<option>3 out of 4 Days</option>
</select>";
}else if($days=='3 out of 4 Days'){
echo "
<select name=\"ticket_type[]\">
<option value="">Select one...</option>
<option>1 Day</option>
<option>2 Days</option>
<option selected=\"selected\">3 out of 4 Days</option>
</select>";
}else{
echo "
<select name=\"days[]\">
<option value="">Select one...</option>
<option>1 Day</option>
<option>2 Days</option>
<option>3 out of 4 Days</option>
</select>";
}?>
I was wondering what the best way to run two(or more) else if statements on one PHP page. This is part of a form where its looking for a ticket type and how many days. I need to run two of these statement on one page and I doesn't seem to work because once it hit a true statement it stops. How can I get this to work?
<?php
if($ticket_type=='Child (5 & Under)'){
echo "
<select name=\"ticket_type[]\">
<option value=\"\">Select one...</option>
<option selected=\"selected\">Child (5 & Under)</option>
<option>Child (6 - 12)</option>
<option>Teen (13 - 17)</option>
</select>";
}else if($ticket_type=='Child (6 - 12)'){
echo "
<select name=\"ticket_type[]\">
<option value=\"\">Select one...</option>
<option>Child (5 & Under)</option>
<option selected=\"selected\">Child (6 - 12)</option>
<option>Teen (13 - 17)</option>
</select>";
}else if($ticket_type=='Teen (13 - 17)'){
echo "
<select name=\"ticket_type[]\">
<option value=\"\">Select one...</option>
<option>Child (5 & Under)</option>
<option>Child (6 - 12)</option>
<option selected=\"selected\">Teen (13 - 17)</option>
</select>";
}else{
echo "
<select name=\"ticket_type[]\">
<option value=\"\">Select one...</option>
<option>Child (5 & Under)</option>
<option>Child (6 - 12)</option>
<option>Teen (13 - 17)</option>
</select>";
}?>
<?php
if($days=='1 Day'){
echo "
<select name=\"ticket_type[]\">
<option value="">Select one...</option>
<option selected=\"selected\">1 Day</option>
<option>2 Days</option>
<option>3 out of 4 Days</option>
</select>";
}else if($days=='2 Days'){
echo "
<select name=\"ticket_type[]\">
<option value="">Select one...</option>
<option>1 Day</option>
<option selected=\"selected\">2 Days</option>
<option>3 out of 4 Days</option>
</select>";
}else if($days=='3 out of 4 Days'){
echo "
<select name=\"ticket_type[]\">
<option value="">Select one...</option>
<option>1 Day</option>
<option>2 Days</option>
<option selected=\"selected\">3 out of 4 Days</option>
</select>";
}else{
echo "
<select name=\"days[]\">
<option value="">Select one...</option>
<option>1 Day</option>
<option>2 Days</option>
<option>3 out of 4 Days</option>
</select>";
}?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以极大地简化代码:
这样,您只有一处指定选择选项,因此如果您将来需要添加更多选项,只需更改该处即可。现在,您必须更改 if() 块的每个部分,这将变得乏味且容易出错。
You could simplify the code immensely:
This way, you have only ONE place where the select options are specified, so if you need to add more in the future, you only change that one place. Right now you'd have to change every single section of your if() block, which'll get tedious and error prone.
哦天哪...你为什么把事情搞得这么复杂?
但你真的应该考虑使用 ID,这样你就不必比较字符串。
oh boy... why you make things so complicated ?
but you really should consider using ID's so you dont have to compare strings.
哎呀,这有点乱。您应该阅读 PHP 的替代语法。
Yikes, that's kind of messy. You should read up on PHP's Alternative Syntax.