简体   繁体   English

更好的方法在PHP中执行此块

[英]better way of doing this block in php

I feel like there has got to be a better way of doing this to populate the selected.... 我觉得必须有一个更好的方法来填充选定的....

<p>
    <label for="industry" class="medium">Industry</label>
    <select name="industry" >
        <option value="" selected="<?php if($_POST['industry'] =="") { echo "selected";} ?>">-- Select Industry --</option>
        <option value="Retail" selected="<?php if($_POST['industry'] =="Retail") { echo "selected";} ?>">Retail</option>
        <option value="Restaurant" selected="<?php if($_POST['industry'] =="Restaurant") { echo "selected";} ?>">Restaurant</option>
        <option value="Salon" selected="<?php if($_POST['industry'] =="Salon") { echo "selected";} ?>">Salon</option>
        <option value="Pizza Delivery" selected="<?php if($_POST['industry'] =="Pizza Delivery") { echo "selected";} ?>">Pizza Delivery</option>
        <option value="Grocery" selected="<?php if($_POST['industry'] =="Grocery") { echo "selected";} ?>">Grocery</option>
        <option value="Quick Service" selected="<?php if($_POST['industry'] =="Quick Service") { echo "selected";} ?>">Quick Service</option>
        <option value="Liquor Store" selected="<?php if($_POST['industry'] =="Liquor Store") { echo "selected";} ?>">Liquor Store</option>
        <option value="Tobacco" selected="<?php if($_POST['industry'] =="Tobacco") { echo "selected";} ?>">Tobacco</option>
        <option value="Video Store" selected="<?php if($_POST['industry'] =="Video Store") { echo "selected";} ?>">Video Store</option>
        <option value="Other" selected="<?php if($_POST['industry'] =="Other") { echo "selected";} ?>">Other</option>
    </select>
</p>

You can create array with values like 您可以使用类似值创建数组

$options = array(
    'Retail', 'Restaurant', 'Salon'
);

Then do simple for to output values into form 然后做简单for输出值成形式

<select name="industry">
    <?php for ($i = 0; $i < count($options); $i++) { ?>
    <option value="<?php echo $options[$i]; ?>"<?php echo $_POST['industry'] == $options[$i] ? ' selected="selected"' : ''; ?>><?php echo $options[$i]; ?></option>
    <?php } ?>
</select>

By creating the $selected array you cut out the visual and computational faff of having to check $_POST each time. 通过创建$selected数组,您可以减少每次检查$_POST的视觉和计算方法。

$selected = array();
$selected[$_POST['industry']] = "selected='selected'";
//all others will be nothing
$industries = array(); //populate with options

foreach($industries as $i){
  echo "<option value='$i' ".$selected[$i].">$i</option>";
}

I would simply make an array of the option values and loop through it and echo out the needed code. 我只是创建一个选项值数组并循环遍历它并回显所需的代码。 It's a bit cleaner to look at. 看起来有点干净。

$optionValues = //blag

foreach($optionValues as $optionValue)
   echo //option tag stuff
<p>
    <label for="industry" class="medium">Industry</label>
    <select name="industry" >
        <option value="">-- Select Industry --</option>
        <?php
            $Industries = Array
            (
                "Retail",
                "Restaurant",
                "Salon",
                "Pizza Delivery",
                "Grocery",
                "Quick Service",
                "Liquor Store",
                "Tobacco",
                "Video Store",
                "Other"
            );

            foreach($Industry as $Industries)
            {
                $Selected = ($_POST['industry'] == $Industry) ? 'selected="selected"' : "";
                echo '<option value="' . $Industry . '" ' . $Selected . '>' . $Industry . '</option>'
            }
        ?>
    </select>
</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM