开发者

Method for initializing selectmenu to certain value?

开发者 https://www.devze.com 2023-03-24 02:28 出处:网络
I have a screen that shows users a list of players and the grades users have given them. By clicking on a button, they can sel开发者_JAVA百科ect from a list of players and grade these players themselv

I have a screen that shows users a list of players and the grades users have given them. By clicking on a button, they can sel开发者_JAVA百科ect from a list of players and grade these players themselves.

I want to streamline this process by allowing users to simply click on a player name where it goes to the grading page with the select menu already initialized to the player's name that they just clicked on.

Is there a way to initialize a select menu to a certain value?

Here is the mySQL query:

$query = @mysql_query('SELECT person.firstname, person.lastname, person.id FROM person inner join player ON player.person_id=person.id WHERE player.team_id=' . $homeid . ' ORDER BY lastname asc');

And the code that creates the select menu:

<select name='id'>
    <?php
        while ($temp = mysql_fetch_assoc($query)) {
            echo "<option value=" . $temp['id'] . ">" . htmlspecialchars($temp['firstname']) . " " . htmlspecialchars($temp['lastname']) . "</option>";
        }

    ?>
</select>


of course ,,, The option you want to be selected should contain the attribute selected

 if //bla bla this is the one
   echo "<option selected=\"selected\" value=" . $temp['id'] . ">" . htmlspecialchars($temp['firstname']) . " " . htmlspecialchars($temp['lastname']) . "</option>";
 else
   echo "<option value=" . $temp['id'] . ">" . htmlspecialchars($temp['firstname']) . " " . htmlspecialchars($temp['lastname']) . "</option>";  


if (currentId == $temp['id']) <option selected>...</option>
else <option>...</option>


If you have a value called $id you can:

echo '<option value="'.$temp['id'].'" '.($temp['id'] == $id ? 'selected="selected"' : '').'>'.htmlspecialchars($temp['firstname']).' '.htmlspecialchars($temp['lastname']).'</option>';

Difference:

($temp['id'] == $id ? 'selected="selected"' : '')

The selected attribute on a preselects this value upon loading the page.

I Hope understood your question.

0

精彩评论

暂无评论...
验证码 换一张
取 消