Best way to make this block in php

I feel that there must be a better way to do this in order to fill out 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>
+5
source share
4 answers

By creating an array $selected, you cut out the visual and computational focus to check every time $_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>";
}
+3
source

You can create an array with values, for example

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

Then follow simple forto output values ​​to the form

<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>
+6
source

I would just make an array of option values ​​and scroll through it and highlight the necessary code. It is a little cleaner to watch.

$optionValues = //blag

foreach($optionValues as $optionValue)
   echo //option tag stuff
0
source
<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>
0
source

All Articles