下拉选择基于数据库条目 [英] Dropdown select based on database entries

查看:120
本文介绍了下拉选择基于数据库条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想要一个选择自动填充下拉菜单的是选择,或者否。从数据库查询返回的值将用于评估将选择哪个下拉菜单。

So, I would like a select to auto-populate the dropdown's "Yes" selection, or alternatively, the "No". The returned value from a database query will be used to evaluate which dropdown will be selected.

我认为,因为它是真/假自动填充只写2个条件,但我认为会有一个更好的(阅读:较少凌乱的代码)的方式来编写代码,将根据数据库的结果从下拉列表中自动填充正确的选择。

I have thought since it's a true/false auto-population to just write 2 conditionals, but I thought there would be a better (read: less messy code) way to write code that would auto-populate the right selection from the drop down, based on the result from the database.

我试图写的代码的基础是检查变量对所有的选择,然后附加一个字符串,将选择下拉为用户在视图中。

The base of the code I'm trying to write would be checking the variable against all of the selects, then appending a string that would select that drop down for the user in the view.

我的问题是有一个更简单的方法来做这个,而不是为每个可能的下拉值写条件。

My question would be is there a simpler way to do this, instead of writing conditionals for each possible drop down value?

代码通过请求,写在CodeIgniter PHP:

Code by request, written in CodeIgniter PHP:

 $this->db->select('row'); 
 $result = $this->db->get('table');

 // This just selects and returns the values. This code does work, I'm just looking for a better way to do this task that someone might know, because I'm going to have drop downs with hundreds of possibilities, of which, I want the predefined one to be selected.

 // Assume $result->row = "Yes"

 if ( $result = "Yes" ) {  
    #Code for echo'ing radio button with "Yes" selected
       }

 else {  
    #Code for echo'ing radio button with "No" selected
       }


推荐答案

您检查表单助手?您可以使用 form_radio 并提供您要在第三个参数中选择的值:

Have you check Form Helper in CI? You can use form_radio and supply the value that you want to select in the third parameter:

form_radio('field_name', 'Yes', $result == 'Yes');
form_radio('field_name', 'No', $result == 'No');

第一个参数是字段名称,第二个参数是字段的值,第三个参数是boolean :radio selected,FALSE:未选择)。由于这是一个收音机,字段名称应该相同。

First parameter is field name, Second parameter is value for the field, and third parameter is boolean (TRUE: radio selected, FALSE: not selected). Since this is a radio, the field name should same.

这篇关于下拉选择基于数据库条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆