开发者

Running more then one Else If statement on a page PHP

开发者 https://www.devze.com 2023-04-11 05:43 出处:网络
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 state

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 val开发者_开发技巧ue=\"\">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>";
        }?>


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.


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

<select name="ticket_type[]">
   <option value="">Select one...</option>
   <option <? echo ($ticket_type == 'Child (5 &amp; Under) ? 'selected="selected" : ''; ?>'>Child (5 &amp; 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.


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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号