注意:$ i在代码前面设置,有25个下拉框,数组显示前5个有值,所以我需要将这些下拉设置为选中并显示相关价值。
我有一个包含以下内容的数组:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24] => )
$i=1;
for ($p=1; $p<=25; $p++){
for ($pp=1; $pp<=5; $pp++){
echo "<select id='rqa".$i."".$p."' class='business' name='q".$i."[]' onclick='mand();'>
<option value='' selected='selected'>".$rank."</option>
<option value='".$pp."'";
if (in_array($p, $arr)) echo 'selected';
echo ">".$pp."</option>";
}
}
我要做的就是设置将相关的下拉列表设置为选中它的值。有意义吗?
所有这一切都是将前5个下拉菜单设置为选中,但它们的值为5,而不是1,2,3,4,5。
我遇到的问题
答案 0 :(得分:3)
请尝试以下代码......
$arr = array (1,2,3,4,5,'','','','','','','','','','','','','','','','','','','','');
$i=1;
for ($p=1; $p<=25; $p++){
echo "<br><select id='rqa".$i."".$p."' class='business' name='q".$i."[]' onclick='mand();'>";
for ($pp=1; $pp<=5; $pp++){
$selected = "";
if ($pp == $arr[$p-1]){
$selected = "selected='selected'";
}
echo "<option value='$pp' ". $selected .">".$pp."</option>";
}
echo "</select>";
}
答案 1 :(得分:0)
<?php
$arr = ARRAY(0=>1, 1=>2, 2=>3, 3=>4, 4=>5, 5=>FALSE, 6=>FALSE);
$i=1;
for ($p=1; $p<=25; $p++){
$arr_index = $p-1;
echo "<select id='rqa".$i.$p."' class='business' name='q".$i."[]' onclick='mand();'>";
for ($pp=1; $pp<=5; $pp++) {
//<option value='' selected='selected'>".$rank."</option>
echo "<option value='$pp'";
if (isset($arr[$arr_index]) AND $pp == $arr[$arr_index]) echo " selected='selected'";
echo ">".$pp."</option>";
}
echo "</select>";
echo "<br /><br />";
}
?>
答案 2 :(得分:0)
试试这个:
<?php
$myArray = Array( 1, 2, 3, 4, 5 );
for ($p=1; $p<=25; $p++){
for ($pp=1; $pp<=5; $pp++){
echo "<select id='rqa".$i."".$p."' class='business' name='q".$i."[]' onclick='mand();'>
<option value='' selected='selected'>".$rank."</option>
<option value='".$pp."'";
if (in_array($p, $myArray)) echo 'selected';
echo ">".$pp."</option>";
}
}
?>