Hide or delete entry in dropdown list

I have a dropdown that populates 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 request is used several times in the application, but only in this specific case I would like to exclude the entry where Provider_Name = 'Foo'.

I would prefer not to change it or to include an additional request only for this case, so is there a way to remove an entry from the drop-down list after it has been filled?

+4
source share
1 answer

At runtime, you can skip PHP 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 run jQuery:

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

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

+4
source

Source: https://habr.com/ru/post/1617561/


All Articles