Php 选项值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19884685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:10:28  来源:igfitidea点击:

Php option value

phpselectoption

提问by Wrostran

I have a list of areas (1000+) and i was wondering if there was a way i can do this easier with code instead of repeating each value.

我有一个区域列表(1000+),我想知道是否有一种方法可以用代码更容易地做到这一点,而不是重复每个值。

<select>
<option value="apple" <?php if ($user_data["$area"] == apple){echo 'selected';} ?>>Apple
</option> 

<option value="lemon" <?php if ($user_data["$area"] == lemon){echo 'selected';} ?>>Lemon
</option> 

<option value="orange" <?php if ($user_data["$area"] == orange){echo 'selected';} ?>>Orange
</option> 

<option value="banana" <?php if ($user_data["$area"] == banana){echo 'selected';} ?>>Banana
</option>
</select>

I.E. have the same piece of php code for each option instead of having to type in the name of the value each time

IE 对每个选项都有相同的 php 代码,而不必每次都输入值的名称

<?php if ($user_data["$area"] == option VALUE){echo 'selected';} ?>

Could you provide some code or ideas for what to look in tutorials, i have no idea how to start. Thank you!

您能否提供一些代码或想法以了解教程中的内容,我不知道如何开始。谢谢!

回答by Cristian Cavalli

//pseudo
$arr = array("apple", "lemon", "orange", ...);
foreach($arr as $value) {
    echo '<option value="'.$value;
    if($user_data[$area] === $value) {
        echo 'selected';
    }
    //echo {the end of your option field syntax}
}

回答by jerdiggity

All the solutions look good... Here's one more way though:

所有的解决方案看起来都不错……不过,还有一种方法:

<select>
<?php
  $areas = array('apple', 'lemon', 'orange', 'banana');
  $areas_count = count($areas);
  for ($i = 0; $i < $areas_count; $i++) {
    echo '<option value="' . $areas[$i] . '"';
    echo ($user_data[$area] == $areas[$i]) ? ' selected' : '';
    echo '>' . ucwords($areas[$i]) . '</option>';
  }
?>
</select>

回答by SirDarius

Use an array:

使用数组:

$areas = array(
    'apple' => 'Apple',
    'lemon' => 'Lemon',
    'orange' => 'Orange',
    'banana' => 'Banana'
);

Then use that array to print the select:

然后使用该数组打印选择:

<select>
<?php foreach ($areas as $value => $text): ?>
    <option value="<?php echo $value; ?>" <?php if ($user_data[$area] == $value) {echo 'selected';} ?>><?php echo $text; ?>
    </option> 
<?php endforeach; ?>
</select>

I am using an associative array because I am assuming that you want to be able to customize the areas' text label, and that they will not only be a capitaized version of the value used to match the user data.

我使用关联数组是因为我假设您希望能够自定义区域的文本标签,并且它们不仅是用于匹配用户数据的值的大写版本。

回答by Niladri Banerjee - Uttarpara

foreach ($areas as $key => $val)
{
    $select.= '<option '; // opening the option tag
    foreach ($selected_from_db as $keyselected => $valselected)
    {
      $val_fetch_from_array = $valselected == $val['campaignid'] ? 'selected' : '';
      $select.= $val_fetch_from_array; // codes for selecting multiple values
    }
    $select.= ' value = "' . $val['campaignid'] . '">#' . $val['campaignid'] . '-' . $val['campaignname'] . '</option>'; // closing the option tag
}