Dawson
Dawson

Reputation: 573

Hide or delete an entry in a dropdown list

I have a dropdown which is populated by a MySQL query

while ($myrow = mysql_fetch_array($phresult)) 
{
    if ($ID == $myrow["Cmpy_ID"])
    {
        printf("
    <option value=\"%s\" selected>%s</option>\n", $myrow["Cmpy_ID"], $myrow["Provider_Name"]);
    }
    else
    {
        printf("
    <option value=\"%s\">%s</option>\n", $myrow["Cmpy_ID"], $myrow["Provider_Name"]);
    }
}

This query is used several times in the application but only in this one certain case I'd like to exclude the entry where Provider_Name = 'Foo'.

I'd prefer not to change it or include an extra query just for this case, so is there a way to remove an entry from the dropdown after it has been populated?

Upvotes: 4

Views: 67

Answers (1)

Thamilhan
Thamilhan

Reputation: 13313

During PHP execution, you can skip when the value is Foo:

<?php
while ($myrow = mysql_fetch_array($phresult)) 
{
    if($myrow["Provider_Name"]=='Foo')
            continue;

    if ($ID == $myrow["Cmpy_ID"])
    {
        printf("
    <option value=\"%s\" selected>%s</option>\n", $myrow["Cmpy_ID"], $myrow["Provider_Name"]);
    }
    else
    {
        printf("
    <option value=\"%s\">%s</option>\n", $myrow["Cmpy_ID"], $myrow["Provider_Name"]);
    }
}

Or go by JQuery:

$("#selector option[value='Foo']").remove();

By the way mysql is deprecated, go for mysqli or PDO.

Upvotes: 4

Related Questions