Jump to content

default value of dropdown from query.


cdoyle

Recommended Posts

I'm not sure if this is really a PHP or HTML question, but hopefully it's a simple one.

 

Basically I have a page,  that displays a specific person's information. 

In my user_table I have a field labeled as 'active'  0 for not active  1 for active.

 

So on this page,  I want to create a yes/no dropdown and have it display the current status.

So I started to create a simple html dropdown,  but I'm not sure how to get it to display the current status as the default..

 

<select name="active">
    <option value="1">Yes</option>
    <option value="0">No</option>
</select>

 

$getstaff1['active']  <<< it will either be a 1 or a 0.

This is the variable from my query that is ran when the page is opened. 

I'm not sure how to use this, to make the dropdown display the correct status.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/197259-default-value-of-dropdown-from-query/
Share on other sites

Perhaps this can help.  Not the best code I've ever written but it should work in your case.

 

$select_field = "<select name='active'>";
if(getstaff1['active']   == 1)
{
    $active_1 = " selected";
    $active_0 = "";
}
else if(getstaff1['active']   == 0)
{
    $active_1 = "";
    $active_0 = " selected";
}
else
{
    $active_1 = "";
    $active_0 = "";
}

$select_field .= "    
        <option value='1' $active_1>Yes</option>
        <option value='0' $active_0>No</option>";

$select_field .= "</select>";

 

andrewgauger definitely has the better answer if you're able to do inline PHP.

 

<select name="active">
    <option value="1" <?php if($getstaff1['active']==1) echo "SELECTED"?>>Yes</option>
    <option value="0" <?php if($getstaff1['active']==0) echo "SELECTED"?>>No</option>
</select>

thank you!
I knew it had to be something simple like that!

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.