dongyi5070 2016-07-29 06:18
浏览 64
已采纳

根据数据库提取值设置选定值

Case :

Hi, Stackoverflow community.

I want to set selected value on SELECT type input based on fetched value from database table column. So if db table column contain "A", SELECT type input will also show "A".

Value in SELECT type input, provide manually.

Experiment :

To show my logic on how to do this, i tried this code

// $query2['via'] will produce fetched value from database
<select selected='".$query2['via']."' name='slPayment'>
<option>--</option>
<option value='Cash'>Cash</option>
<option value='Bank Transfer'>Bank Transfer</option>
<option value='Credit Card'>Credit Card</option>
<option value='Cheque'>Cheque</option>
<option value='Others'>Others</option>
</select>

Experiment Result :

Wont Work

Desired Output :

I actually just want that SELECT type input, show the exactly same value from fetched database value. Any idea how to fix this ?

Thank You

Thank you for Stackoverflow community. I won't stop saying thank you. Btw, i purely asking for learning and knowledge sharing. I'm open to any comment (related info with this question).

But please don't judge me like i am just asking for code. I actually learn how to code based on that code itself, then i adapt to make use of that code properly, am not familiar with library, am still a college student. I wont ask before i try it by myself. Some people did judge me on recent question. And it's not very nice act to this community. I'm learning, we learning.

So thank you once again.

  • 写回答

2条回答 默认 最新

  • duanjie9630 2016-07-29 06:32
    关注

    You can change your code thus:

    <select name='slPayment'>
        <option>--</option>
        <option " . (($query2['via']=='Cash') ? 'selected="selected"': '') .  "value='Cash'>Cash</option>
        <option " . (($query2['via']=='Bank Transfer') ? 'selected="selected"': '') . " value='Bank Transfer'>Bank Transfer</option>
        <option " . (($query2['via']=='Credit Card') ? 'selected="selected"': '') . " value='Credit Card'>Credit Card</option>
        <option " . (($query2['via']=='Cheque') ? 'selected="selected"': '') . " value='Cheque'>Cheque</option>
        <option " . (($query2['via']=='Others') ? 'selected="selected"': '') . " value='Others'>Others</option>
    </select>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备