任何建议如何做同样的结果打印不同的选项与选择如下面的代码,但更短?
<?php
if($UsrRow['Usr'] == '0'){
?>
<option value="0" selected="selected">a</option>
<option value="1">b</option>
<option value="2">c</option>
<option value="3">d</option>
<option value="4">e</option>
<option value="5">f</option>
<?php
}else if($UsrRow['Usr'] == '1'){
?>
<option value="0">a</option>
<option value="1" selected="selected">b</option>
<option value="2">c</option>
<option value="3">d</option>
<option value="4">e</option>
<option value="5">f</option>
}
?>
...
答案 0 :(得分:3)
你能试试吗,
<?php
$Options = array(0=>'a', 1=>'b',2=>'c',3=>'d',4=>'e');
foreach($Options as $key=>$val){
$selected = "";
if($UsrRow['Usr'] == $key){
$selected = " selected";
}
echo "<option value='$key' $selected >$val</option>";
}
?>
答案 1 :(得分:1)
您可以使用内联条件执行此操作:
<option value="0" <?php if($UsrRow['Usr'] == '0') echo 'selected="selected"'; ?>>a</option>
<option value="1" <?php if($UsrRow['Usr'] == '1') echo 'selected="selected"'; ?>>b</option>
<option value="2" <?php if($UsrRow['Usr'] == '2') echo 'selected="selected"'; ?>>c</option>
<option value="3" <?php if($UsrRow['Usr'] == '3') echo 'selected="selected"'; ?>>d</option>
<option value="4" <?php if($UsrRow['Usr'] == '4') echo 'selected="selected"'; ?>>e</option>
<option value="5" <?php if($UsrRow['Usr'] == '5') echo 'selected="selected"'; ?>>f</option>
答案 2 :(得分:1)
创建一个数组
$arr = array('0'=>'a','1'=>'b','2'=>'c','3'=>'d','4'=>'e','5'=>'f');
foreach($arr as $k=>$v){
$sel = "";
if($UsrRow['Usr'] == $k){
$sel = " selected ";
}
echo "<option value='$k' $sel >$k</option>";
}
答案 3 :(得分:0)
试试这个
$arr = array('a','b','c','d','e','f');
$list='';
foreach($arr as $value)
{
for($i=0;$i<=5;$i++)
{
if($UsrRow['Usr'] == $i)
{
$list.= '<option value="<?php echo $i; ?>" selected="selected"><?php echo $value; ?></option>';
}
else
{
$list.= '<option value="<?php echo $i ?>"><?php echo $value; ?></option>';
}
}
}
答案 4 :(得分:0)
<?php
$options = '';
$users = array(
0 => 'a',
1 => 'b',
//all users here
);
foreach ($users as $id => $name) {
$options .= '<option value="' . $id . '" ';
$options .= ($UsrRow['Usr'] == $id) ? 'selected="selected">' : '>';
$options .= $name . '</option>';
}
echo $options;
答案 5 :(得分:0)
<?php
if($UsrRow['Usr'] == '0')
$select0='selected="selected"';
else if($UsrRow['Usr'] == '1')
$select1='selected="selected"';
else if($UsrRow['Usr'] == '2')
$select2='selected="selected"';
else if($UsrRow['Usr'] == '3')
$select3='selected="selected"';
else if($UsrRow['Usr'] == '4')
$select4='selected="selected"';
else if($UsrRow['Usr'] == '5')
$select5='selected="selected"';
?>
<option value="0" <?=$select0?>>a</option>
<option value="1" <?=$select1?>>b</option>
<option value="2" <?=$select2?>>c</option>
<option value="3" <?=$select3?>>d</option>
<option value="4" <?=$select4?>>e</option>
<option value="5" <?=$select5?>>f</option>