2011-03-10 76 views
5

做這個塊我覺得有一定是這樣選擇的填充一個更好的方式方法....更好的在PHP

<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> 
+2

看看這個:http://stackoverflow.com/questions/5249904/how-to-populate-listbox-drpdown-box-select-box-from-php-array/5249998#5249998 – amosrivera 2011-03-10 20:56:22

+0

我使用CodeIgniter。他們有一個form_helper將爲你做這個。 :-D – 2011-03-10 20:58:57

回答

3

通過創建$selected數組你切出的視覺並且每次都必須檢查$_POST的計算faff。

$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>"; 
} 
+0

smart再次感謝.. – Trace 2011-03-10 22:32:26

6

您可以創建值的數組一樣

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

然後做簡單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> 
+0

每次檢查'$ _POST'還是需要大量的gumph。 – fredley 2011-03-10 20:59:57

+0

嗯,它的確如此,但是我喜歡乍看之下發生了什麼。在你的回答中,我不得不想一下,看看'$ selected'數組是什麼樣的。但有人喜歡他習慣的技巧:) – rdamborsky 2011-03-10 21:11:09

0

我只想通過做期權價值和循環數組它並回顯出所需的代碼。看起來更清潔一點。

$optionValues = //blag 

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