在 PHP 页面上运行多个 Else If 语句

发布于 2024-12-08 12:44:53 字数 3019 浏览 0 评论 0原文

我想知道在一个 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 &amp; 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 &amp; 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 &amp; 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 &amp; 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪少女 2024-12-15 12:44:53

您可以极大地简化代码:

$options = array(
   'Select One' => 'Select One',
   '1 Day' => '1 Day',
   '2 Days' => '2 Days',
   '3 or more' => '3 or More'
);

echo '<select name="ticket_type[]">';
foreach($options as $key => $val) {
   $select = (($key == $days) ? ' selected="selected"' : '');
   echo "<option value=\"", htmlspecialchars($key), "\"$select>", htmlspecialchars($val), "</option>";
}
echo '</select>';

这样,您只有一处指定选择选项,因此如果您将来需要添加更多选项,只需更改该处即可。现在,您必须更改 if() 块的每个部分,这将变得乏味且容易出错。

You could simplify the code immensely:

$options = array(
   'Select One' => 'Select One',
   '1 Day' => '1 Day',
   '2 Days' => '2 Days',
   '3 or more' => '3 or More'
);

echo '<select name="ticket_type[]">';
foreach($options as $key => $val) {
   $select = (($key == $days) ? ' selected="selected"' : '');
   echo "<option value=\"", htmlspecialchars($key), "\"$select>", htmlspecialchars($val), "</option>";
}
echo '</select>';

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.

善良天后 2024-12-15 12:44:53

哦天哪...你为什么把事情搞得这么复杂?

<select name="ticket_type[]">
   <option value="">Select one...</option>
   <option <? echo ($ticket_type == 'Child (5 & Under) ? 'selected="selected" : ''; ?>'>Child (5 & Under)</option>
   <option <? echo ($ticket_type == 'Child (6 - 12) ? 'selected="selected" : ''; ?>>Child (6 - 12)</option>
   <option <? echo ($ticket_type == 'Child (13 - 17) ? 'selected="selected" : ''; ?>>Teen (13 - 17)</option>
</select>";

但你真的应该考虑使用 ID,这样你就不必比较字符串。

oh boy... why you make things so complicated ?

<select name="ticket_type[]">
   <option value="">Select one...</option>
   <option <? echo ($ticket_type == 'Child (5 & Under) ? 'selected="selected" : ''; ?>'>Child (5 & Under)</option>
   <option <? echo ($ticket_type == 'Child (6 - 12) ? 'selected="selected" : ''; ?>>Child (6 - 12)</option>
   <option <? echo ($ticket_type == 'Child (13 - 17) ? 'selected="selected" : ''; ?>>Teen (13 - 17)</option>
</select>";

but you really should consider using ID's so you dont have to compare strings.

伴我心暖 2024-12-15 12:44:53

哎呀,这有点乱。您应该阅读 PHP 的替代语法

<?php if ($condition): ?>
This is a test..
<?php endif; ?>

Yikes, that's kind of messy. You should read up on PHP's Alternative Syntax.

<?php if ($condition): ?>
This is a test..
<?php endif; ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文