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?

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>';
?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.