Jump to content

how to show selected result from db on dropdown?


lovephp

Recommended Posts

friend the code display all the managers name

 

  <?php
        echo '<select name="Manager">';
        $query="SELECT * FROM managers";
        $result = mysql_query($query);
        while ($row = mysql_fetch_array($result)) {
            echo "<option value='" . $row['manager_name'] . "'>" . $row['manager_name'] . "</option>";
        }
        echo '</select>';
        ?>

 

and the variable below displays the result

 

$Manager= $row['Manager'] ;  

 

but how do i do it on the above dropdown list the it shows which result is in the $Manager variable?

Link to comment
Share on other sites

You could try something like this:

<?php
echo '<select name="Manager">';
$query="SELECT * FROM managers";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
    echo "<option value='{$row['manager_name']}'";
    if($Manager == $row['manager_name']) { echo ' selected="selected"'; }
    echo ">{$row['manager_name']}</option>";
}
echo '</select>';
?>
Link to comment
Share on other sites

I'm not sure I understand what you mean. Is the code you posted working or not? If it's not working, what does it do?

 

And why do you want to use $row['Manager']?  What's inside the brackets [] is supposed to be the name of a column coming from a table in your Database. Do you have column namde 'Manager' in your 'Managers' table?

 

If its the case (but I doubt it, but whatever, I'll take the guess), then your code would need to be like this:

echo "<option value='" . $row['Manager'] . "'>" . $row['Manager'] . "</option>";

OR

$Manager = $row['Manager'];
echo "<option value='" . $Manager . "'>" . $Manager . "</option>";

seems like what you asked for, but I'm not sure it makes sense :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.