doujia2463 2013-09-25 14:03
浏览 145
已采纳

在PHP中保存下拉列表的正确方法

I am using 2 kinds of dropdowns. The Yes/No and one with multiple values. Either way I use this:

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

or

<select id="active">
    <option value="1">Choice 1</option>
    <option value="2">Choice 2</option>
    <option value="3">Choice 3</option>
    <option value="4">Choice 4</option>
</select>

The value of the Yes/No I store as a bit (before as tinyint) in my mysql-db. The multiple-choice I store as int. I get the values out of my object $member and try to select the right value:

<option value="0" <?php echo $member->active == 0 ? 'selected="selected"' : 'selected="unselected"'; ?>>Yes</option>
<option value="1" <?php echo $member->active == 1 ? 'selected="selected"' : 'selected="unselected"'; ?>>Yes</option>

This works perfectly when I have the multiple choice stored as int but not with the Yes/No stored as bit or tinyint.

What am I doing wrong? The value of $member->active are right. I know Yes/No can be done with a checkbox but I won't this sorted out first.

Thx

  • 写回答

1条回答 默认 最新

  • doufen1890 2013-09-25 14:16
    关注

    To provide an answer and assuming you are already loading the appropriate data and storing it into whatever class/object $member is.

    <option value="0" <?php echo $member->active == 0 ? 'selected="selected"' : ''; ?>>No</option>
    <option value="1" <?php echo $member->active == 1 ? 'selected="selected"' : ''; ?>>Yes</option>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料