donglanche9257 2017-01-30 02:34
浏览 82
已采纳

PHP / HTML在sql查询的下拉列表中显示选定的选项

I wasn't sure how to title my question, I'm basically asking if anyone knows a better way to approach this. I'm using this function below to show which option is pulled from the DB, I'm wondering if there is a more compact way or better way to do this other than elseif each ID

function access_option($access)
{
    if ($access == 0)
    {
        echo '<option value="0" selected>Member</option>
        <option value="1">Streamer</option>
        <option value="2">Moderator</option>
        <option value="3">Manager</option>
        <option value="4">Administrator</option>
        <option value="5">Senior Administrator</option>';
    }
    elseif ($access == 1)
    {
        echo '<option value="0">Member</option>
        <option value="1" selected>Streamer</option>
        <option value="2">Moderator</option>
        <option value="3">Manager</option>
        <option value="4">Administrator</option>
        <option value="5">Senior Administrator</option>';
    }
    elseif ($access == 2)
    {
        echo '<option value="0">Member</option>
        <option value="1">Streamer</option>
        <option value="2" selected>Moderator</option>
        <option value="3">Manager</option>
        <option value="4">Administrator</option>
        <option value="5">Senior Administrator</option>';
    }
    elseif ($access == 3)
    {
        echo '<option value="0">Member</option>
        <option value="1">Streamer</option>
        <option value="2">Moderator</option>
        <option value="3" selected>Manager</option>
        <option value="4">Administrator</option>
        <option value="5">Senior Administrator</option>';
    }
    elseif ($access == 4)
    {
        echo '<option value="0">Member</option>
        <option value="1">Streamer</option>
        <option value="2">Moderator</option>
        <option value="3">Manager</option>
        <option value="4" selected>Administrator</option>
        <option value="5">Senior Administrator</option>';
    }
    elseif ($access == 5)
    {
        echo '<option value="0">Member</option>
        <option value="1">Streamer</option>
        <option value="2">Moderator</option>
        <option value="3">Manager</option>
        <option value="4">Administrator</option>
        <option value="5" selected>Senior Administrator</option>';
    }
}

Is this the best way to do it?

  • 写回答

3条回答 默认 最新

  • dpafea04148 2017-01-30 02:37
    关注

    You can do it with less repetition using ternary operators:

    function access_option($access)
    {
        echo '<option value="0"'.($access == 0 ? ' selected' : '').'>Member</option>';
        echo '<option value="1"'.($access == 1 ? ' selected' : '').'>Streamer</option>';
        echo '<option value="2"'.($access == 2 ? ' selected' : '').'>Moderator</option>';
        echo '<option value="3"'.($access == 3 ? ' selected' : '').'>Manager</option>';
        echo '<option value="4"'.($access == 4 ? ' selected' : '').'>Administrator</option>';
        echo '<option value="5"'.($access == 5 ? ' selected' : '').'>Senior Administrator</option>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?