开发者

Creating a default list item when returning values from a Database

开发者 https://www.devze.com 2023-03-04 00:17 出处:网络
I\'m trying to code a script so that a user can click a dropdown menu so they can place where they would like a certain picture to go (It\'s a picture albums selection).

I'm trying to code a script so that a user can click a dropdown menu so they can place where they would like a certain picture to go (It's a picture albums selection).

I've ran into a problem, I have a dropdown box in my PHP script, but I need it to select the option that the user has previously selected via an ID in the database.

So basically I have an album id, this id will return the albums name, the name will be shown to the user, then converted into an ID again, and determening on what album the user clicked on in the dropdown will determine which id is stored in the database. So when I return the ID from the database, I need the default selection of the dropdown box to be whatev开发者_Python百科er the user selected before hand. this is my code:

<?php

echo "<select>";  

$query = mysql_query("SELECT * FROM albums");  

while($row = mysql_fetch_assoc($query))  
{

$option = $row['albumname'];  
$id = $row['id'];  

<option value="$id">$option</option>  

}  

echo "</select>";  

?>

Help Would be appreciated.


You cold do it with a session or a cookie.

Or create a table with the user_id and last selected album id and fetch that. Update that id after the selection again.


While you're repeating through and writing your dropdown, check if the selected ID matches the ID of each dropdown - if it does, add selected="selected"

while($row = mysql_fetch_array($query)) {
    $selected = "";
    if($row['id'] == $selectedId) $selected = ' selected="selected"';
    echo '<option value="' . $row['id']?> . '"' . $selected . '>' . 
        $row['albumname'] . '</option>';
}
0

精彩评论

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