我有这个函数应该回显从 while 循环返回的完整选项值。问题是未设置 SELECT 属性。因此,即使传递了不同的 id 变量,列表选项也会默认为第一个选项。
function popselect($pos){
$pquery = "SELECT * FROM `players` WHERE `position` = '$pos'";
$presult = mysql_query($pquery)
or die ("Query failed: " . mysql_error());
$tquery = "SELECT * FROM teams WHERE `selection_id` = '$id'";
$tresult = mysql_query($tquery)
or die ("Query failed: " . mysql_error());
$trow = mysql_fetch_array($tresult);
while (($prow = mysql_fetch_array($presult)) != null){
$fullname = $prow['name'].' '.$prow['surname'];
$selected = "";
if ($fullname == $trow['f']){
$selected = 'selected="selected"';}
if ($fullname == $trow['gk']){
$selected = 'selected="selected"';}
if ($fullname == $trow['ld']){
$selected = 'selected="selected"';}
if ($fullname == $trow['rd']){
$selected = 'selected="selected"';}
if ($fullname == $trow['lm']){
$selected = 'selected="selected"';}
if ($fullname == $trow['rm']){
$selected = 'selected="selected"';}
echo "<option value='{$fullname}' ".$selected.">".$fullname."</option>";
}
}
html如下:
<fieldset>
<ul>
<li>
<div id="l"><label for="gk">Goalkeeper:</label></div>
<div id="s"><select name="gk">
<?php popselect(Goalkeeper)?>
</select></div>
</li>
<li>
<div id="l"><label for="f">Forward:</label></div>
<div id="s"><select name="f">
<?php popselect(Forward); ?>
</select></div>
</li>
<li>
<div id="l"><label for="lm">Left Midfielder:</label></div>
<div id="s"><select name="lm">
<?php popselect("Left Midfielder"); ?>
</select></div>
</li>
<li>
<div id="l"><label for="rm">Right Midfielder:</label></div>
<div id="s"><select name="rm">
<?php popselect("Right Midfielder"); ?>
</select></div>
</li>
<li>
<div id="l"><label for="ld">Left Defender:</label></div>
<div id="s"><select name="ld">
<?php popselect("Left Defender"); ?>
</select></div>
</li>
<li>
<div id="l"><label for="rd">Right Defender:</label></div>
<div id="s"><select name="rd">
<?php popselect("Right Defender"); ?>
</select></div>
</li>
</ul>
</fieldset>