从sql返回值设置选项选择属性

时间:2013-10-06 04:19:07

标签: php mysql html-form

我有这个函数应该回显从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>

1 个答案:

答案 0 :(得分:0)

问题是可变范围......

我的功能无法访问$ id ...

$tquery = "SELECT * FROM teams WHERE `selection_id` = '$_GET['selection_id']'";    

做了一个魅力!