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.

图片转代码服务由CSDN问答提供 功能建议

案例:

您好,Stackoverflow社区 。

我想根据数据库表列中的提取值在SELECT类型输入上设置选定值。 因此,如果db表列包含“A”,则SELECT类型输入也将显示“A”。

SELECT类型输入中的值,手动提供。

实验:

要显示我如何执行此操作的逻辑,我尝试了此代码

  /  / $ query2 ['via']将从数据库中生成获取的值
&lt; select selected ='“。$ query2 ['via']。”'name ='slPayment'&gt; 
&lt; option&gt;  - &lt; / 选项&gt; 
&lt;期权值='现金'&gt;现金&lt; /选项&gt; 
&lt;期权值='银行转帐'&gt;银行转帐&lt; /选项&gt; 
&lt;期权值='信用卡'&gt;信用卡&lt;  ; / option&gt; 
&lt; option value ='Check'&gt; Check&lt; / option&gt; 
&lt; option value ='Others'&gt; Others&lt; / option&gt; 
&lt; / select&gt; 
   
 
 

实验结果:

无法工作

所需输出:< / strong>

我实际上只是希望SELECT类型输入,从获取的数据库值中显示完全相同的值。 知道如何解决这个问题吗?

谢谢

感谢Stackoverflow社区。 我不会停止说谢谢。 顺便说一句,我纯粹要求学习和知识分享。 我愿意接受任何评论(与此问题相关的信息)。

但请不要评判我,就像我只是要求代码一样。 我实际上学习了如何基于代码本身进行编码,然后我适应了正确使用该代码,不熟悉库,我仍然是一名大学生。 在我亲自尝试之前我不会问。 有人在最近的问题上判断我。 对这个社区来说,这不是一件好事。 我正在学习,我们正在学习。

再次感谢你。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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>
    
    点赞 评论
  • dsf6565 2016-07-29 06:26

    img_type is my query result and i want img_name in drop_down

     <select id="id_name"  name="photo_type" value="">
                      <option value=""></option>
                       <?php foreach($img_types as $row){ ?>
                        <option value="<?php echo $row->imgtype_id?>"><?php echo $row->imgtype_name?></option>
                        <?php } ?>
                    </select>
    
    点赞 评论

相关推荐 更多相似问题